Database Reference
In-Depth Information
RAND(UNIX_TIMESTAMP())
RAND(CONNECTION_ID())
RAND(UNIX_TIMESTAMP()+CONNECTION_ID())
However, it's probably better to use other seed value sources if you have them. For
example, if your system has a /dev/random or /dev/urandom device, read the device and
use it to generate a value for seeding RAND() .
How Random Is RAND()?
Does the RAND() function generate evenly distributed numbers? Check it out for yourself
with the following Python script, rand_test.py , from the stats directory of the recipes
distribution. (That directory also contains equivalent scripts in other languages.) The
script uses RAND() to generate random numbers and constructs a frequency distribution
from them, using 10 categories (“buckets”). This provides a means of assessing how
evenly distributed the values are:
#!/usr/bin/python
# rand_test.pl: create a frequency distribution of RAND() values.
# This provides a test of the randomness of RAND().
# Method: Draw random numbers in the range from 0 to 1.0,
# and count how many of them occur in .1-sized intervals
import cookbook
npicks = 1000 # number of times to pick a number
bucket = [ 0 ] * 10 # buckets for counting picks in each interval
conn = cookbook . connect ()
cursor = conn . cursor ()
for i in range ( 0 , npicks ):
cursor . execute ( "SELECT RAND()" )
( val ,) = cursor . fetchone ()
slot = int ( val * 10 )
if slot > 9 :
slot = 9 # put 1.0 in last slot
bucket [ slot ] += 1
cursor . close ()
conn . close ()
# Print the resulting frequency distribution
for slot , val in enumerate ( bucket ):
print ( " %2d %d " % ( slot + 1 , val ))
 
Search WWH ::




Custom Search