ID:               43807
 Updated by:       [EMAIL PROTECTED]
 Reported By:      martin dot kober at wu-wien dot ac dot at
-Status:           Open
+Status:           Bogus
 Bug Type:         Arrays related
 Operating System: Linux 2.6
 PHP Version:      5.2.5
 New Comment:

array_rand() uses the same algorithm as shuffle().
And I can't see any problems with both functions.


Previous Comments:
------------------------------------------------------------------------

[2008-01-10 17:55:39] martin dot kober at wu-wien dot ac dot at

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";
}




------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=43807&edit=1

Reply via email to