|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2008-01-12 15:31 UTC] tony2001@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 13:00:02 2025 UTC |
Description: ------------ The keys that array_rand returns are uniformly distributed, but their order is not: Counting the 0, 1 and 2 of the sample code output, I consistently get some distribution like this: Overall count (this is fine): 0 1 2 649 694 657 Count per position (this is wrong): 0 1 2 [0] 500 181 319 [1] 149 513 338 0 is way too often in [0] (same for 1 in [1]), there must be some problem with the algorithm. Adding a shuffle() is a workaround, but this is a potential trap for users depending on an even distribution. There are some similar bugs in the DB, but they are all very old and seem to be Windows-related. Reproduce code: --------------- for ($i = 1; $i <= 1000; $i++) { echo implode(" ", array_rand(array(1,2,3), 2)), "<br>\n"; }