#19000 [NEW]: array_rand not returning a new set of random values on reloading the PHP page.

From: Date: Tue, 20 Aug 2002 21:50:20 +0000
Subject: #19000 [NEW]: array_rand not returning a new set of random values on reloading the PHP page.
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-17353@lists.php.net to get a copy of this message
From: 007@pathcom.com Operating system: Windows 2000 PHP version: 4.2.2 PHP Bug Type: Arrays related Bug description: array_rand not returning a new set of random values on reloading the PHP page. 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> -- Edit bug report at http://bugs.php.net/?id=19000&edit=1 -- Try a CVS snapshot: http://bugs.php.net/fix.php?id=19000&r=trysnapshot Fixed in CVS: http://bugs.php.net/fix.php?id=19000&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=19000&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=19000&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=19000&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=19000&r=support Expected behavior: http://bugs.php.net/fix.php?id=19000&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=19000&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=19000&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=19000&r=globals

« previous php.bugs (#17353) next »