Reshuffle at Regular Intervals

Posted Sat, 19 Nov 2005

I added a subtle feature to my blog this morning; my tag cloud now shuffles every hour. I did this by seeding the random number system using the current hour and shuffling the tags in my tagcloud, like so:

srand( Time::now.hour )
tags = tags.sort{ |a,b| rand() <=> rand() }

The result is that with each hour my tag cloud takes on a new form, and there will be 24 different combinations.

If you wanted to shuffle every hour and not repeat every 24 hours, you could also seed using the decimal representation of the current time rounded to the previous hour:

now = Time::now
srand( ( now - (now.min * 60 + now.sec) ).to_i )

Related Stories

Post a Comment




About

My name is Tim Fanelli, I am a software engineer in Northern NY. I spend most of my time working, and when I can, I try to post interesting things here.

Cigar Dossiers