From:             martin dot kober at wu-wien dot ac dot at
Operating system: Linux 2.6
PHP version:      5.2.5
PHP Bug Type:     Arrays related
Bug description:  array_rand doesn't return keys in random order

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 bug report at http://bugs.php.net/?id=43807&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=43807&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=43807&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): 
http://bugs.php.net/fix.php?id=43807&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=43807&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=43807&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=43807&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=43807&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=43807&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=43807&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=43807&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=43807&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=43807&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=43807&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=43807&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=43807&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=43807&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=43807&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=43807&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=43807&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=43807&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=43807&r=mysqlcfg

Reply via email to