note 63828 added to function.mt-rand

From: Date: Sat, 01 Apr 2006 02:50:56 +0000
Subject: note 63828 added to function.mt-rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-107112@lists.php.net to get a copy of this message
Gerry (or "Garry" ;>) here again . I was concerned about the implications of barnettjw's findings, so I emailed him to see if I could get the code. It would appear that the reason he got the results he did, was because his code was imploding his array of 50,000 numbers without specifying a glue (easy mistake to make). Because of this the glue was defaulting to '' and just making one long number. Then when he looked for occurrences of 1, the numbers 12-19 were also counted and the number 11 twice. The same happened with the number 2, but less so because the string only contained numbers 20-25. To confirm this you can add up all the occurrences in his first example and you will see that they add up to 109,843 instead of 50,000. I have made modifications to my previous example, to take into consideration barnettjw's alterations. For me, in the results of the following example, all numbers have an approximately equal number of occurrences: <?php $iterations = 100000; $min = 0; $max = 50; $testVals = array_fill($min, $max+1, 0); $zeroPadStr = '%0'. strlen($iterations) .'d'; $zeroPadStr2 = '%0'. strlen($max) .'d'; echo 'Results from '. $iterations .' "mt_rand(0, 2)" executions...'."<br>\n<br>\n"; for($i=0; $i<$iterations; $i++){ $randNum = mt_rand($min, $max); $testVals[$randNum]++; } foreach($testVals As $testVal => $occurnaces){ $percent = $occurnaces / $iterations * 100; $paddedTestVal = sprintf($zeroPadStr2, $testVal); echo "<br>\n $paddedTestVal occurred $occurnaces times ($percent%)"; } ?> As well as sending me his code, barnettjw also passed along a link to http://www.random.org/ , which is a for anybody who doesn't want random numbers to be generated by a computer, but instead by physical elements in our environment. I'm sure there are people who can make use of this interesting service, perhaps to please a particularly fussy client. :D ---- Server IP: 66.163.161.117 Probable Submitter: 202.89.167.189 ---- X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER, HTML_MESSAGE autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.mt-rand.php Edit -- http://master.php.net/note/edit/63828 Del: integrated -- http://master.php.net/note/delete/63828/integrated Del: useless -- http://master.php.net/note/delete/63828/useless Del: bad code -- http://master.php.net/note/delete/63828/bad+code Del: spam -- http://master.php.net/note/delete/63828/spam Del: non-english -- http://master.php.net/note/delete/63828/non-english Del: in docs -- http://master.php.net/note/delete/63828/in+docs Del: other reasons-- http://master.php.net/note/delete/63828 Reject -- http://master.php.net/note/reject/63828 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#107112) next »