Braindisorder If you are not free to choose wrongly and irresponsibly, you are not free at all.

23Jan/110

How random is random?

Out of curiosity if the random number functions rand() and mt_rand() in PHP were really random enough I set my quadcore to work and generated 1 billion random numbers between 1 and 10 (inclusive) for each function. I could've made a small random generation script that would simply count occurances, but I thought it would be fun to use MySQL to store the values. A 45 GB database was the result.

[image title="phpmysqladmin-rand" size="large" id="1584" align="center" linkto="default" ]

Using rand()

1 100.013.861 % 9,9986
2 99.998.344 % 10,0002
3 99.998.180 % 10,0002
4 100.001.634 % 9,9998
5 100.003.286 % 9,9997
6 99.991.275 % 10,0009
7 99.991.538 % 10,0008
8 99.999.388 % 10,0001
9 100.010.714 % 9,9989
10 99.991.780 % 10,0008
1.000.000.000 % 100,0000

Using mt_rand

1 99.999.557 % 10,0000
2 100.007.901 % 9,9992
3 99.995.652 % 10,0004
4 100.005.155 % 9,9995
5 99.986.239 % 10,0014
6 100.002.680 % 9,9997
7 100.006.429 % 9,9994
8 100.009.602 % 9,9990
9 99.990.255 % 10,0010
10 99.996.530 % 10,0003
1.000.000.000 % 100,0000

As you can see, random is pretty random. At least, random enough.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.