note 92226 added to function.srand

From: Date: Tue, 14 Jul 2009 23:43:12 +0000
Subject: note 92226 added to function.srand
Groups: php.notes 
Request: Send a blank email to php-notes+get-158311@lists.php.net to get a copy of this message
This is a reply to the comment from php at capuzza dot net He/she suggest to use something "random" like loading Google to seed rand, I would not recommend this. Note, I am not a cryptologist! When I use rand in PHP5 and run it 1000000 times to create a random number with min 0 and max 100 I see the result pretty evenly split up. The way it should be since generating rand with min 0 and max 1 (50% chance) will give me 0 about 500000 times and 1 about 500000 times. BUT if I follow the suggestion of php at capuzza dot net and seed with the microsecond() after a page load I see that my numbers are rather predicable. When I run rand 100 times with min 0 and max 100 I get mostly zeros and only a few numbers with a value, so the same number is generated because loading a webpage usually takes about the same time. Running the code below with $min=0; $max=100; $runs=100; $with_google = true; Will create the same 8 numbers over and over again from 100 possible numbers. All other numbers where never generated and had a count of 0!!!! This is from a home DSL connection which should be more random then a dedicated server pipe. The same code, without seeding first, with these settings: $min=0; $max=100; $runs=100; $with_google = false; Will generate a list of 61 numbers with only 39 numbers which where never generated. As you can see, the built in seed generates a "more random" number. Here is the code so you can try it out yourself. Warning, I had to break the line containing the google.com URL with a line break to pass the filter on PHP.net. Just goto the term+ part and remove the line break to make the code work... <?PHP /* Random number test Run from command line with php -n <filename> if you set $runs to something really big */ $min = 50; //Min number for rand $max = 100; //Max number for rand $runs = 100; //Max runs to generate random numbers $with_google = false; // true $a_track = array(); //Ensure every possible value is set to zero to avoid notices for( $x=$min; $x <= $max; $x++ ) $a_track[$x] = 0; //Time to run rand() as many times as requested for( $x=0; $x < $runs ; $x++ ) { if( $with_google == true ) { //Suggested by php at capuzza dot net for more "randomness" //You can test other ways to make rand "more random" in this block.. $foo=file_get_contents("http://www.google.com/search?q=search+ term+does+not+matter&hl=en"); list($usec, $sec) = explode(" ", microtime()); srand((int)($usec*10)); } $r = rand($min,$max); $a_track[$r] = $a_track[$r] + 1; } //All done, output overview echo "Out of $runs runs\n"; for( $x=$min; $x <= $max; $x++ ) echo "$x was created {$a_track[$x]} times \n"; ?> ---- Server IP: 212.124.37.9 Probable Submitter: 92.75.88.117 ---- Manual Page -- http://www.php.net/manual/en/function.srand.php Edit -- https://master.php.net/note/edit/92226 Del: integrated -- https://master.php.net/note/delete/92226/integrated Del: useless -- https://master.php.net/note/delete/92226/useless Del: bad code -- https://master.php.net/note/delete/92226/bad+code Del: spam -- https://master.php.net/note/delete/92226/spam Del: non-english -- https://master.php.net/note/delete/92226/non-english Del: in docs -- https://master.php.net/note/delete/92226/in+docs Del: other reasons-- https://master.php.net/note/delete/92226 Reject -- https://master.php.net/note/reject/92226 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158311) next »