Voting

: min(four, nine)?
(Example: nine)

The Note You're Voting On

fasaxc at yahoo dot com
22 years ago
The best way to ensure a random seed is to do the following:
To start:
1) get your initial seed with mt_srand(microtime() * 1000000)
2) generate a random no. $random=mt_rand()
3) save this number in a file (or database or whatever so that it is available next time the page is loaded)

Now, for each time your script is loaded :
1) load the value you saved above and do $new_seed=($random+(microtime() * 1000000))%pow(2,32)
2) mt_srand($new_seed);
3) generate a new random no. $random=mt_rand()
4) save that number back in the file/database

This procedure takes advantage not only of the randomness of microtime() but of all the previous calls to microtime() so your seed becomes better and better with time. It also generates good seeds even on platforms where microtime() doesn't take all the values it can.

Just using microtime() * 1000000 only results in 1000000 possible seeds (and less on some platforms as noted) - the function above gives 2^32 seeds with an avelanche effect accross multiple executions.

<< Back to user notes page

To Top