note 16986 added to function.mt-srand
| From: | ajith at wiredisland dot com | Date: | Wed, 21 Nov 2001 02:38:54 +0000 |
| Subject: | note 16986 added to function.mt-srand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-21092@lists.php.net to get a copy of this message | ||
You can use a combination of mt_rand(), array_push() and in_array() to get few unique randum numbers
out of small range of numbers ( eg: 1-10 ). Otherwise you would get dups when dealing with small
ranges.
<?php
function random_number($min, $max) {
return mt_rand($min, $max);
}
$wins = array();
while ( count ( $wins ) < 3 ) {
$value = random_number(1,10);
if (in_array($value, $wins)) {
continue;
}
array_push($wins, $value);
}
echo $wins[0]." ". $wins[1]. " ".$wins[2];
?>
--
http://www.php.net/manual/en/function.mt-srand.php
http://master.php.net/manage/user-notes.php?action=edit+16986
http://master.php.net/manage/user-notes.php?action=delete+16986
http://master.php.net/manage/user-notes.php?action=reject+16986