Bug #67233 [NEW]: 5.5.12 array rand is not random

From: Date: Thu, 08 May 2014 09:05:02 +0000
Subject: Bug #67233 [NEW]: 5.5.12 array rand is not random
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-185710@lists.php.net to get a copy of this message
From: bugsphp077 at nabialek dot org Operating system: Windows 8.1 PHP version: 5.5.12 Package: Arrays related Bug Type: Bug Bug description:5.5.12 array rand is not random 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 bug report at https://bugs.php.net/bug.php?id=67233&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=67233&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=67233&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=67233&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=67233&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=67233&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=67233&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=67233&r=needscript Try newer version: https://bugs.php.net/fix.php?id=67233&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=67233&r=support Expected behavior: https://bugs.php.net/fix.php?id=67233&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=67233&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=67233&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=67233&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=67233&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=67233&r=dst IIS Stability: https://bugs.php.net/fix.php?id=67233&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=67233&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=67233&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=67233&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=67233&r=mysqlcfg

« previous php.bugs (#185710) next »