note 47586 deleted from function.rand by sean

From: Date: Mon, 22 Nov 2004 15:14:45 +0000
Subject: note 47586 deleted from function.rand by sean
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80868@lists.php.net to get a copy of this message
Note Submitter: NOTICE: rand() is not truly random. ---- The rand() function is not truly random without the use of the srand() function. The best way to show you this is by an example of course. Read the comments after the script first, then run the script! The script: <?php $rand= NULL; //I do NOT recomend values of $i above 100,000. Resource use will be high. for($i= 0; $i < 100000; $i++) { for($j= 0; $j < 10; $j++) { //srand(); //Uncomment this line for better radomness $rand.= rand(0, 9); } $tempAr[]= $rand; $rand= NULL; } $first= $tempAr[0]; foreach($tempAr as $key=> $digitSet) { if($first == $digitSet) { echo '"'.$first.'" at key 0 matches "'.$digitSet.'" at key '.$key."\n"; } } ?> What it does? - It generates 100,000 numbers 10 digits long. Each digit in each number is randomly generated using the digits between 0 & 9. The script then stores each result and later examines all the results to see if the first number generated is repeated anywhere again. This is the output of the script when I executed it: (Your output will most likely be different) "8019036706" at key 0 matches "8019036706" at key 0 "8019036706" at key 0 matches "8019036706" at key 16384 "8019036706" at key 0 matches "8019036706" at key 32768 "8019036706" at key 0 matches "8019036706" at key 49152 "8019036706" at key 0 matches "8019036706" at key 65536 "8019036706" at key 0 matches "8019036706" at key 81920 "8019036706" at key 0 matches "8019036706" at key 98304 What does it mean? - Well... it means exacly what it says. The number 8019036706, which was the first number generated and stored in the array at position 0, occurs again at a later position(s) in the array. What does it really mean? - It means that rand alone is not random and it repeats. How so? Take a look at the difference in keys. It is constant in all. (i.e. 32768 - 16384 =16384, 49152- 32768= 16384 ...etc.) Run the script few times and see for yourself. Note that every time you run the script you begin with a new random number. However, the positions in the array it occurs are constant for all executions, and hence the difference between them. Now uncomment the srand(); line and run the script agian. If there is more then one matches found (the first match will always match itself), you should play the lottery. The theory? - The probability of a number like the once generated above repeating again is 1 in 10,000,000,000 (10^10). When the script was run without the srand(); uncommented the script generated 6 matches, that is 6 in 100,000. Lucky script? Every time? In fact the array from 0 to 16384 is identical to the array from 16384 to 32768, 32768 to 49152 etc.. hence rand() without srand(), repeats at constant intervals and therefore is not random. Side note: No software can currently produce a truly random number. Remember that when cracking the lottery. :) Regards, *drenintell* drenintell at _ g m a i l_d o t _ c o m ---- Reason: bad code

« previous php.notes (#80868) next »