RE: [PHP] Picking random numbers
| From: | Jason Murray | Date: | Tue, 21 Aug 2001 01:17:50 +0000 |
| Subject: | RE: [PHP] Picking random numbers | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-63594@lists.php.net to get a copy of this message | ||
> OK I want to pick 5 random numbers from 1 to 200. Easy.
> They must all be different. Not so easy.
<?
srand ((double) microtime() * 1000000);
while (count($selectednumbers) < 5)
{
$randomnumber = rand(1, 200);
if (!in_array($randomnumber, $selectednumbers))
{
$selectednumbers[] = $randomnumber;
}
}
//$selectednumbers is now an array with 5 items between 1 and 200.
?>
This should work well enough to give you the general idea ... sorry if it
doesn't, I grabbed some old code and modified it so I didn't embarrass
myself too much by posting spaghetti code. :)
Jason
--
Jason Murray
jasonm@melbourneit.com.au
Web Developer, Melbourne IT
"Work now, freak later!"