From:             
Operating system: 
PHP version:      5.3.5
Package:          Arrays related
Bug Type:         Bug
Bug description:array_rand doesn't like the 31st value of an array

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

)

-- 
Edit bug report at http://bugs.php.net/bug.php?id=54235&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=54235&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=54235&r=trysnapshot53
Try a snapshot (trunk):              
http://bugs.php.net/fix.php?id=54235&r=trysnapshottrunk
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=54235&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=54235&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=54235&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=54235&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=54235&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=54235&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=54235&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=54235&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=54235&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=54235&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=54235&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=54235&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=54235&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=54235&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=54235&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=54235&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=54235&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=54235&r=mysqlcfg

Reply via email to