php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19000 array_rand not returning a new set of random values on reloading the PHP page.
Submitted: 2002-08-20 17:50 UTC Modified: 2002-08-20 22:57 UTC
From: 007 at pathcom dot com Assigned:
Status: Closed Package: Arrays related
PHP Version: 4.2.2 OS: Windows 2000
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: 007 at pathcom dot com
New email:
PHP Version: OS:

 

 [2002-08-20 17:50 UTC] 007 at pathcom dot com
PHP installed with php-4.2.2-installer.exe.

The code in the manual produced a set of random values that did not change on reloading the page in IE 5.01.
If however the "num_required" argument was changed in the code, a new set of random values was generated.

The following code with my own "array_random()" function works perfectly. I will use this code until I see the fix for array_rand().

<html>
    <head>
        <title>Example</title>
    </head>
    <body>

<pre>
<?php
$Array = array(array("http://www.yahoo.com","yahoo.gif","Yahoo"),
               array("http://www.internet.com","internet.com.gif","Internet.com"),
               array("http://www.cnn.com","cnn.gif","CNN"),
               array("http://www.php.net","php.gif","PHP"),
               array("http://www.google.com","google.gif","Google"),
               );
print_r($Array);
?>
</pre>
<?php
#srand ((float) microtime() * 10000000); # This seeds the random number generator.
function array_random($array_name,$random_elements){
  global $random_key; # This is the $random_key array.
  $x=count($array_name)-1; # 0 to $x ($x is the number of elements in the $array_name array) is the range of the random number we will generate for each element in the $random_key array.
  
  for ($i=0;$i<$random_elements;$i++){# First we give to each element in the $random_key array a random but not necessarily unique value.
    $random_key[$i]=rand(0,$x);
  }
  while (count(array_unique($random_key))<count($random_key)){ # Then we check the $random_key array values for uniqueness, and keep generating random values for each index until they're all unique.
    for ($i=0;$i<$random_elements;$i++){
      $random_key[$i]=rand(0,$x);
    }
  }
  print_r($random_key);
  print "<br>";
}
array_random($Array,2); # The first argument is the array form which we want to generate x (the second argument) number of random elements; x must set to 1 or more.
print "\$random_key[1] value is : $random_key[1]";
?>
<table>
 <tr>
  <td><a href="<?=$Array[$random_key[0]][0]?>"><img src="images/<?=$Array[$random_key[0]][1]?>"></a></td><td><a href="<?=$Array[$random_key[1]][0]?>"><img src="images/<?=$Array[$random_key[1]][1]?>"></a></td>
 </tr>
 <tr>
  <td><?=$Array[$random_key[0]][2]?></td><td><?=$Array[$random_key[1]][2]?></td>
 </tr>
</table> 


    </body>
</html>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-08-20 17:57 UTC] edink@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.
 [2002-08-20 22:54 UTC] 007 at pathcom dot com
I installed PHP with php-4.2.2-installer.exe.
How can I implement the fix made for this bug in CVS?
 [2002-08-20 22:57 UTC] kalowsky@php.net
Please don't re-open the bug to ask for help backporting patches.

You won't be able to apply any patch without the source, and a compiler.  So using the installer.exe is of no good.

You can either download the latest unstable build and use that or, or wait until a new stable release.  
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 20:01:29 2024 UTC