RE: [PHP] random numbers
| From: | Jon Haworth | Date: | Tue, 12 Nov 2002 10:17:33 +0000 |
| Subject: | RE: [PHP] random numbers | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-123954@lists.php.net to get a copy of this message | ||
Hi Tamas,
> > <?php
> > for ($i=1; $i<=6; $i++)
> > $x[] = mt_rand(1, 90);
> > foreach ($x as $current)
> > echo $current. "<br />";
> > ?>
>
> Thanks for help, but your program may generate same numbers between
> the 5 generated numbers, hovewer I want to generate different numbers
> such as lottery-numbers.
That's not a problem: just add a couple of lines to test for existence:
$i = 0;
while ($i<6) {
$pick = mt_rand(1, 90);
if (!in_array($pick, $x)) {
$x[] = $pick;
$i++;
}
}
> But if this is the only solution I'll do it in this way.
It's *never* the only solution :-)
Cheers
Jon