Random Function
| From: | Teren | Date: | Fri, 14 Nov 2003 18:05:45 +0000 |
| Subject: | Random Function | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-169663@lists.php.net to get a copy of this message | ||
Hi, I have the following function that generates a random number, but the key is it only generates
the number once within the range you specify. So, if you want a range of 5 numbers, it will give you
5 different numbers. My problem is the function was set to start at 0, but I need it to start at 1,
so i made a change to the function (line 9) and now the script only works every now and then. Can
somebody look at it and see if they see why it would be doing that? Thanks
<?php
$s = array();
$n = 3;
function set_num() {
File random0.php saved.
$ cat random0.php
<?php
$s = array();
$n = 3;
function set_num() {
global $s, $n;
$add = "yes";
$ran = rand(1, $n);
if(count($s) > 0) {
foreach($s as $sh) {
if($ran == $sh) {
$add = "no";
}
}
}
if($add == "yes") {
$s[] = $ran;
} else {
set_num();
}
}
while(count($s) <= $n) {
set_num();
}
?>
Teren