Randomization for Message of the Day (or whatever else)


#1

To create a DAILY randomization of WHATEVER (i.e. MOTD)
First create a file (perhaps random.txt)
Keep 2 lines in it… The first is the the daily random number , the second is the unix timestamp (of the time last updated.)

something like :

1 1046960000

Then you can do something like:
[php]
// set to whatever the maximum possible events to randomize
$maxnumber = 1200;

// Path to the file with the random info
$filename = “/path/to/random.txt”;

$randomlength = 86400; //Time length between randomization.

// open file to get data
$contents = file($filename);

// If the currently stored random number is greater
// than 1 day old get a new one and update the file
if ($contents[‘1’] <= (time()-$randomlenght)) {
$number=rand(1,$maxnumber);
$handle = fopen ($filename, “w”);
$results = fwrite ($handle, $number.“n”);
$results = fwrite ($handle, time().“n”);
fclose ($handle);
} else {
//if the number is less than 1 day old… Use it.)
$number = $contents[‘0’];
}
[/php]

Now you can use $number (which is randomized daily) to select perhaps a quote of the day, a message of the day, a thought of the day or whatever.

To change the randomness from say 1 day to half a day, adjust the $randomlength appropriately (in seconds). In which this case would be 43200.