#19000 [Opn->Csd]: array_rand not returning a new set of random values on reloading the PHP page.
| From: | edink@php.net | Date: | Tue, 20 Aug 2002 21:57:36 +0000 |
| Subject: | #19000 [Opn->Csd]: array_rand not returning a new set of random values on reloading the PHP page. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-17354@lists.php.net to get a copy of this message | ||
ID: 19000
Updated by: edink@php.net
Reported By: 007@pathcom.com
-Status: Open
+Status: Closed
Bug Type: Arrays related
Operating System: Windows 2000
PHP Version: 4.2.2
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2002-08-20 17:50:19] 007@pathcom.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>
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=19000&edit=1