View Full Version : Generate random number>
Hi,
What is the best way to generate a random number within a range?
Thanks
mlampkin
09-17-2006, 12:07 AM
The simplest ( and fairly decent ) way would simply be to use the random() function using something like the current time as the seed...
If you want to get 'fancy' and potentially generate a better distribution... you can still use random() but in addition to setting the seed, also set the state array... both of these can be done in a single step using the initstate() function...
Btw...
If you are doing things the second way... make sure your state is 256 bytes to give the best distributions... also you really want to load it with GOOD random data... a source of which could be had by doing things such as reading data from /dev/random if its available on the target platform...
There are of course other random number generator functions... those are just two common methods...
Michael
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.