From:             cdturri at hotmail dot com
Operating system: Windows 2000 Advanced Server SP4
PHP version:      5.0.3
PHP Bug Type:     Arrays related
Bug description:  array_rand() is not returning random values

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] [EMAIL PROTECTED]
You forgot to call srand() to generate random key. Hence the lack of
random data.
[17 Nov 2003 2:25pm CET] [EMAIL PROTECTED]
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.

-- 
Edit bug report at http://bugs.php.net/?id=31453&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=31453&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=31453&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=31453&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=31453&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=31453&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=31453&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=31453&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=31453&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=31453&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=31453&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=31453&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=31453&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=31453&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=31453&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=31453&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=31453&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=31453&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=31453&r=float
No Zend Extensions:          http://bugs.php.net/fix.php?id=31453&r=nozend
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=31453&r=mysqlcfg

Reply via email to