Bug #67233 [Opn->Dup]: 5.5.12 array rand is not random
| From: | levim@php.net | Date: | Thu, 08 May 2014 14:18:01 +0000 |
| Subject: | Bug #67233 [Opn->Dup]: 5.5.12 array rand is not random | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-185713@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67233&edit=1
ID: 67233
Updated by: levim@php.net
Reported by: bugsphp077 at nabialek dot org
Summary: 5.5.12 array rand is not random
-Status: Open
+Status: Duplicate
Type: Bug
Package: Arrays related
Operating System: Windows 8.1
PHP Version: 5.5.12
Block user comment: N
Private report: N
New Comment:
This is well known, but sadly undocumented. Here's a bug report from 2001: https://bugs.php.net/bug.php?id=12312
It also appears to be more severe on Windows: https://bugs.php.net/bug.php?id=45301
I'm closing this as a duplicate of the latter bug.
Previous Comments:
------------------------------------------------------------------------
[2014-05-08 09:05:01] bugsphp077 at nabialek dot org
Description:
------------
---
From manual page: http://www.php.net/function.array-rand
---
Function array_rand does not choose random value. In documentation there is info that there's
no need to use srand or mt_srand any more. In fact using srand function before launching randTest
function (in test script) solve this issue. However using mt_srand function (both with and without
seed) doesn't solve this issue
Test script:
---------------
<?php
$letters =
array('a','b','c','d','e','f','g','h');
$array = array();
// mt_rand used just for filling array, it may be filled manually if neccessary
for ($i=0; $i< 200; ++$i) {
$array[] = $letters[mt_rand(0,count($letters)-1)];
}
for ($i=0; $i< 10000; ++$i) {
echo randTest($array)."<br />";
}
function randTest($array) {
$out = array();
while (count($array) > 0) {
$index = array_rand($array,1);
$out[] = $array[$index];
unset($array[$index]);
}
return implode('', $out);
}
Expected result:
----------------
All lines should be random or at least no visible pattern of repetition should be possible to find
Actual result:
--------------
It repeats its results in test script each 14 times. I mean in output line 1 is the same as line 14,
line 2 is the same as line 15, line 3 is the same as line 16 so it means it is not random at all.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67233&edit=1