php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19343 array_rand does not return keys randomly
Submitted: 2002-09-10 14:09 UTC Modified: 2002-11-20 01:00 UTC
Votes:5
Avg. Score:3.6 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:3 (100.0%)
From: catch at sonaware dot net Assigned:
Status: No Feedback Package: Arrays related
PHP Version: 4.2.3 OS: WinXP
Private report: No CVE-ID: None
 [2002-09-10 14:09 UTC] catch at sonaware dot net
I called array_rand($my_array, count($my_array)). I expected array_rand to return the keys of $my_array in random order. I actually received the keys in almost the same order each time. $my_array was a 4 element array and the third element's key never changed position. I tried using srand also, but it produced same results. On an older version of php(4.1.2) the function worked as expected.

script on WinXP php4.2.3:
$numbers = range (1,4);
$rand_keys = array_rand ($numbers, count($numbers));
foreach ($rand_keys as $val) {
   print $numbers[$val]."\n";
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-09-17 22:08 UTC] iliaa@php.net
Sorry, but the bug system is not the appropriate forum for asking
support questions. 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

Thank you for your interest in PHP.

I've tried this a few dosen times and the results were quite random. Please keep in mind that with a small array, such as the one in your example, there are only 24 possible combinations. Which means, that on average every 24th run you'll get an 'unsorted' array.
 [2002-10-04 17:23 UTC] matthias dot thalmann at sciontec dot com
Well - I experienced the same problems (array_rand() is [still] not random) in a Windows environment as catch@sonaware.net does. I actually was looking forward to a new release as this 'bug' was reported to be corrected and found the 4.2.3 release to act the same wrong way than 'before'.

This must be a problem on the Windows side, for in the Linux environment I really get random values ...
 [2002-11-04 16:34 UTC] catch at sonaware dot net
I see that another person has reproduced the bug. In my original submission I specified my OS as Windows XP. iliaa@php.net may have not tested the function on windows, therefore I think it warrants reopening. My example is sufficient to quickly reproduce the bug.
 [2002-11-04 18:43 UTC] iliaa@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php4-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php4-win32-latest.zip

I've just tested this function using latest CVS (unstable) in Windows XP. The code from your example has worked properly. Therefor I am closing the bug report.
 [2002-11-20 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over 2 weeks, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 08 16:01:27 2024 UTC