Weekly changing random number

Simple task: How do we generate a random number that automatically changes every certain period of time. Of course, we use the week number when calculating the random number. Also, we have to cache a “large integer” that will not change until the end of the week to get a deterministic random-enough number.

The result can look something like this when we integrate it in a WordPress menu and we only take into account the tags with a minimum number of posts.

$the_tags = get_tags();
foreach ($the_tags as $t) {
if ($t->count>INFOCUS_MIN) $ft[] = $t;
$tag = $ft[abs(SOME_LARGE_INTEGER * date(“W”) % sizeof($ft))];
$link = get_tag_link($tag->term_id);
$infocus = ‘<li id=”menu-item-featured-tag” class=”menu-item”><a href=”‘.$link.'”>’ . __(‘In focus: ‘)
. $tag->name.'</a></li>’;

This might be useful and hooking this simple stuff into a menu doesn’t really justify a plugin. But it can be extended with other functionality. We can call it Magic Menu Items. It could integrate with Jetpack to access statistics, and so on.

Leave a Reply

Your email address will not be published. Required fields are marked *