|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2011-03-12 00:27 UTC] felipe@php.net
-Status: Open
+Status: Bogus
[2011-03-12 00:27 UTC] felipe@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 15 09:00:01 2025 UTC |
Description: ------------ There is an odd behavior with array_rand: if the array has 32 or more values, then the 31st value is less likely to be "picked" than the others (the more values the array has, the less it is noticeable). This has been tested and confirmed on different versions of PHP, including 5.3.5 and 5.2.17. Test script: --------------- <?php $nbElements = 50; $test = array(); for ($i = 1; $i <= $nbElements; $i++) { $test[$i] = 0; } for ($i = 0; $i < 100000; $i++) { $test[array_rand($test)]++; } asort($test); echo '<pre>'; print_r($test); echo '</pre>'; ?> Expected result: ---------------- A (more or less) equal distribution between all the values (with the example given above, ~ 2,000 for each value). Actual result: -------------- Every time, the 31st value has been picked half what it "should" have been (meaning, to continue with the same example, around 1,000 times for the 31st value, where for all the others it's approximatively between 1,900 and 2,200): Array ( [31] => 1006 [10] => 1886 … [44] => 2152 )