note 32621 deleted from function.rand by aidan
| From: | aidan@php.net | Date: | Sat, 11 Sep 2004 07:43:14 +0000 |
| Subject: | note 32621 deleted from function.rand by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76454@lists.php.net to get a copy of this message | ||
Note Submitter: stuartc1
----
Here is a simple function that will produce a list of UNIQUE random numbers within a range.
<?
// set some globals
$s = array();
$n = 12; // the range you require (this will give 1-12)
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();
}
}
// call the function as needed
while(count($s) < $n) {
set_num();
}
// just output a list seperated by whitespace
foreach($s as $sh) {
echo $sh." ";
}
?>