php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54235 array_rand doesn't like the 31st value of an array
Submitted: 2011-03-11 23:47 UTC Modified: 2011-03-12 00:27 UTC
From: guenhwyvar dot 68781 at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.5 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: guenhwyvar dot 68781 at gmail dot com
New email:
PHP Version: OS:

 

 [2011-03-11 23:47 UTC] guenhwyvar dot 68781 at gmail dot com
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
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-12 00:27 UTC] felipe@php.net
-Status: Open +Status: Bogus
 [2011-03-12 00:27 UTC] felipe@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

No bug here.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Jul 31 20:01:24 2021 UTC