php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61963 array_rand seed use up
Submitted: 2012-05-06 11:42 UTC Modified: 2012-05-11 11:46 UTC
From: bsnxff at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.12 OS: window xp sp3
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bsnxff at gmail dot com
New email:
PHP Version: OS:

 

 [2012-05-06 11:42 UTC] bsnxff at gmail dot com
Description:
------------
run test_array_rand.php


Test script:
---------------
<?php
$array = array_fill(0, 1000, 'a');
echo rand(0 , 10000);
function ref(&$data = false)
{
    global $array;
    $data = &$array;
    $a = randArray($data);
}
function randArray($data)
{
    global $array;
    //may be wrong here
    $array[] = array_rand($array, 1);
    return $data[rand(0, sizeof($data) - 1)];
}
$i = 1000;
while ($i--)
    ref()
    
// Hi refresh it 3 - 10 times
// you will see
// random number no longer random

// 1 return 10
// 2 return 4453
// 3 return 1251
// 4 return 10
// 5 return 4453
// 6 return 1251
// ...
?>

Expected result:
----------------
make rand(0, 10000) random

Actual result:
--------------
rand(0, 10000) no longer random

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-11 04:04 UTC] bsnxff at gmail dot com
-Summary: array_rand seed no refresh bug +Summary: array_rand seed use up
 [2012-05-11 04:04 UTC] bsnxff at gmail dot com
No one going to take this bug?
 [2012-05-11 04:42 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2012-05-11 04:42 UTC] rasmus@php.net
because it makes no sense and on operating systems with a decent random number 
generator built in there is no bug. Windows XP is not such an operating system. 
You can try mt_rand() and see if you have more luck with that. rand() gives you 
the OS-native pseudo-random generator.
 [2012-05-11 07:17 UTC] bsnxff at gmail dot com
Yes mt_rand works :S
Me bad luck.
 [2012-05-11 07:27 UTC] bsnxff at gmail dot com
Have you tested this case?

rand() will return fixed values not random values.
but mt_rand() works
this is not what user expected.


<?php
$array = array_fill(0, 1000, 'a');
function ref(&$data = false)
{
    global $array;
    $data = &$array;
    $a = randArray($data);
}
function randArray($data)
{
    global $array;
    //may be wrong here
    $array[] = array_rand($array, 1);
    return $data[rand(0, sizeof($data) - 1)];
}
$i = 1000;
while ($i--)
    ref();

//echo mt_rand(0 , 10000);
//echo "\n";
echo $array[rand(0, sizeof($array) - 1)];
?>
 [2012-05-11 07:30 UTC] rasmus@php.net
Yes, I tested your case. On my Linux box I get random values. Like I said, it 
depends on your operating system's pseudo-random generator.
 [2012-05-11 11:46 UTC] bsnxff at gmail dot com
Thanks !
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 03 16:01:29 2025 UTC