#26290 [Bgs]: array_rand() not random
| From: | iliaa@php.net | Date: | Mon, 17 Nov 2003 19:25:11 +0000 |
| Subject: | #26290 [Bgs]: array_rand() not random | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-50495@lists.php.net to get a copy of this message | ||
ID: 26290
Updated by: iliaa@php.net
Reported By: alisovoi at hotmail dot com
Status: Bogus
Bug Type: Performance problem
Operating System: Linux Redhat 9
PHP Version: 4.3.4
New Comment:
Incorrect documentaion I'm afraid.
Previous Comments:
------------------------------------------------------------------------
[2003-11-17 14:19:37] scottm at spamcop dot net
From the array_rand documentation
-----------
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.
------------------------------------------------------------------------
[2003-11-17 14:12:44] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
You forgot to call srand() to generate random key. Hence the lack of
random data.
------------------------------------------------------------------------
[2003-11-17 12:26:00] scottm at spamcop dot net
Can't confirm under same OS. I used a easier test case.
-----
<?php
$random_sayings = array('Hey, this is not','randomly chosen','elements
from an array');
$i = 0;
echo '<pre>';
while ($i < 1000)
{
print $random_sayings[array_rand($random_sayings)] . "\n";
++$i;
}
echo '</pre>';
?>
------------------------------------------------------------------------
[2003-11-17 12:00:36] alisovoi at hotmail dot com
Description:
------------
$array[array_rand($array)] is always the same element of the array.
The server uses php.ini-recommended for the configuration.
(In PHP 4.3.2, this problem is non-existant. PHP 4.3.4 has this bug)
Reproduce code:
---------------
<?php
$random_sayings = array('Hey, this is not','randomly chosen','elements
from an array');
echo($random_sayings[array_rand($random_sayings)]);
?>
Expected result:
----------------
Either one of the 3 elements of the array.
Actual result:
--------------
The same element no matter how many times the user refreshes.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=26290&edit=1