note 79152 added to function.mt-srand

From: Date: Tue, 13 Nov 2007 23:53:32 +0000
Subject: note 79152 added to function.mt-srand
Groups: php.notes 
Request: Send a blank email to php-notes+get-132237@lists.php.net to get a copy of this message
I don't know what Joe is referring to, but the following code never seems to output 'FOO!' however many times I run it: <?php $trials = 1000; $trial_length = 500; $seed = rand(); // Not mt_rand! $s = array_fill(0, $trials, ''); for($j=0; $j<$trials; ++$j) { mt_srand($seed); // Same seed => same results (?) for($i=0; $i<$trial_length; ++$i) { $s[$j] .= chr(mt_rand()); // Exploit the fact that chr($n)==chr($n%256) } } for($j=0; $j<$trials; ++$j) { for($i=$j+1; $i<$trials; ++$i) { if($s[$i]!=$s[$j]) echo "FOO! Different random strings generated from the same seed!\n"; } } $sample = rand(0,$trials-1); echo 0,"\t",base64_encode($s[0]),"\n"; echo $sample,"\t",base64_encode($s[$sample]); ?> ---- Server IP: 203.89.181.186 Probable Submitter: 60.234.168.92 ---- Manual Page -- http://www.php.net/manual/en/function.mt-srand.php Edit -- https://master.php.net/note/edit/79152 Del: integrated -- https://master.php.net/note/delete/79152/integrated Del: useless -- https://master.php.net/note/delete/79152/useless Del: bad code -- https://master.php.net/note/delete/79152/bad+code Del: spam -- https://master.php.net/note/delete/79152/spam Del: non-english -- https://master.php.net/note/delete/79152/non-english Del: in docs -- https://master.php.net/note/delete/79152/in+docs Del: other reasons-- https://master.php.net/note/delete/79152 Reject -- https://master.php.net/note/reject/79152 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#132237) next »