php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31453 array_rand() is not returning random values
Submitted: 2005-01-09 15:04 UTC Modified: 2009-09-27 01:00 UTC
Votes:10
Avg. Score:3.6 ± 1.1
Reproduced:9 of 9 (100.0%)
Same Version:6 (66.7%)
Same OS:6 (66.7%)
From: cdturri at hotmail dot com Assigned:
Status: No Feedback Package: Arrays related
PHP Version: 5CVS, 4CVS (2005-01-10) OS: win32 only
Private report: No CVE-ID: None
 [2005-01-09 15:04 UTC] cdturri at hotmail dot com
Description:
------------
Hi guys,

array_rand() is not returning random values in my scripts. I am using a multidimension array that is being loaded from an INI file with @parse_ini_file(). Tested under Apache v2.0.52/PHP v5.0.3 on Windows 2000 Advanced Server and a CVS build (php5-win32-200501071130.zip) under Windows as well. Bug appears on both. I don't have a Unix system to test it under PHP 5 (shame on me!) but the problem does NOT occur under Unix/Apache v 1.3.33/PHP v4.3.9. 

Calling srand() to seed the random number generator seems to fix the problem but the PHP manual clearly says that:

"Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically."
(http://ie2.php.net/manual/en/function.array-rand.php)

mt_rand(), rand() and mt_srand() also contain notes saying that seeding is not required before calling them.

Found several related bugs, but this one mentions the same issue:

http://bugs.php.net/bug.php?id=26290

[17 Nov 2003 2:12pm CET] iliaa@php.net
You forgot to call srand() to generate random key. Hence the lack of
random data.
[17 Nov 2003 2:25pm CET] iliaa@php.net
Incorrect documentaion I'm afraid.

So, it is one or the other. Either the documentation is wrong or the function is not properly calling seeding the random number generator. Could anyone please have a look?

Thanks for looking at this bug.

Regards,
Christian

Reproduce code:
---------------
I am using this code to verify the bug:

<?php

$multiarray = @parse_ini_file('cache.ini',true);

for($i=0;$i<100;$i++){
	$rand_key = array_rand($multiarray);
    print $rand_key . '<br>';
	if ($i == 1) {
	    $first_item = $rand_key;
	}
	if ($first_item == $rand_key) {
		echo '<br>';
    }
}
?>

You can see the bug at:

http://www.turribeach.com.ar/downloads/test.php

You can download the INI file I am using and the test.php from:

http://www.turribeach.com.ar/downloads/PHP_Test.zip

Expected result:
----------------
A random key from the INI file.

Actual result:
--------------
A key but that it is not random, it is a subset of the original key array.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-10 16:29 UTC] nlopess@php.net
I can confirm this problem.

Maybe the problem is just when running in ZTS mode, as it uses a different seed generator.

However I don't time to trace the problem now.
 [2005-01-10 22:04 UTC] jed@php.net
Present in my Web configuration.
http://jed.homedns.org/test.php

Apache/2.0.52, PHP/5.0.3
Microsoft Windows XP [Version 5.1.2600]

However, *NOT* present in command line. (ZTS?)
 [2005-01-10 22:20 UTC] jed@php.net
Also, is this a Windows-only problem with ZTS? Because apparently, it works under Apache 2 on Linux without a problem. Can someone reproduce this bug on anything but Win32? Is this limited to Apache2, or is it present in IIS as well?
 [2005-01-10 22:28 UTC] cdturri at hotmail dot com
Hi,

Tested under IIS 5.0 and the bug is indeed present. Headers:

 1 HTTP/1.1 200 OK
 2 Server: Microsoft-IIS/5.0
 3 Date: Mon, 10 Jan 2005 21:36:20 GMT
 4 X-Powered-By: ASP.NET
 5 Content-type: text/html
 6 X-Powered-By: PHP/5.0.3

Thanks,
Christian
 [2005-01-10 23:53 UTC] cdturri at hotmail dot com
OK, done my homework. Tested under IIS and Apache on Windows with the CVS builds provided by sniper (PHP4). Bug appeared on BOTH cases. Here are the headers:

 2 Server: Microsoft-IIS/5.0
 3 Date: Mon, 10 Jan 2005 22:45:26 GMT
 4 X-Powered-By: ASP.NET
 6 X-Powered-By: PHP/4.3.11-dev

And:

 2 Date: Mon, 10 Jan 2005 22:49:21 GMT
 3 Server: Apache/2.0.52 (Win32) PHP/4.3.11-dev
 4 X-Powered-By: PHP/4.3.11-dev

As my previous comment I saw no needs to test it under PHP4 with the latest CVS builds in Linux since the bug did not appear on PHP 4.3.9 under Linux. So, we have nailed down the issue to this:

- Bug appears on a Windows System running either IIS or Apache. 
- Bug appears on both PHP4 and PHP5 latest CVS builds. 

Couple of more findings:

- The first key that array_rand() returns on the first run after the Web Server is restarted seems to be random.
- Decreasing the number of keys on the INI file seems to increase the subset of keys that array_rand() incorrectly returns as random keys. 
- Inversely, increasing the number of keys on the INI file seems to decrease the subset of keys that array_rand() incorrectly returns as random keys.

All yours guys!
 [2005-01-11 00:54 UTC] sniper@php.net
I can't reproduce this with Apache2-worker either so it's not ZTS problem. Just some weird windows bug again.

 [2005-11-01 11:30 UTC] sniper@php.net
Please try using this CVS snapshot:

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


 [2005-11-09 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, 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".
 [2006-08-31 22:59 UTC] cdturri at hotmail dot com
Tested on PHP 5.2.0RC3-dev (cli) (built: Aug 30 2006 20:21:41) under Apache 2.2.0/Windows and it is still an issue.
 [2006-12-04 15:14 UTC] iliaa@php.net
Please try using this CVS snapshot:

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


 [2006-12-12 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, 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".
 [2009-09-16 13:42 UTC] ianwladd at gmail dot com
I believe this bug is STILL present in 5.3.0 as I had a script that ran for a while then started generating numbers it had generated before. Can anybody confirm?
 [2009-09-19 11:50 UTC] kopa938 at googlemail dot com
php 5.2.8 / windows xp
same issue, and with srand even ;(
 [2009-09-19 11:52 UTC] pajoye@php.net
Please try using this snapshot:

  http://snaps.php.net/php5.3-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2009-09-27 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, 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: Thu Dec 26 14:01:30 2024 UTC