RE: [PHP] Ahh! Array!!
| From: | Vamsi Nath | Date: | Wed, 02 Aug 2000 19:02:45 +0000 |
| Subject: | RE: [PHP] Ahh! Array!! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-9789@lists.php.net to get a copy of this message | ||
Hi Joe,
Try this code..it worked for me:
<?php
srand((double)microtime()*1000000);
$id = array();
while(count($id) < 6) {
$pick = rand(1,10);
$is_valid = TRUE;
for ($j=0; $j<count($id); ++$j) {
if ($pick == $id[$j]) {
$is_valid = FALSE;
break;
}
}
if($is_valid) {
$id[] = $pick;
}
}
print "Random numbers: ";
while(list(,$val) = each($id)) {
print "$val ";
}
?>
Hope this helps!
--Vamsi
http://php.vamsi.net
-----Original Message-----
From: joe@jWebMedia.com [mailto:joe@jWebMedia.com]
Sent: Wednesday, August 02, 2000 2:32 PM
To: php-general@lists.php.net
Subject: [PHP] Ahh! Array!!
This is driving me nuts!!!!!! It should be SO simple and for some reason
my brain isn't working!!!! I'm selecting records from a database. $num
is the number of rows. I just want to get an array of 6 random numbers,
without getting the same number in the array twice. This is in php3.
I've tried a million variations of this and can't get it to work. Please
help!!!
$i=0;
$id[] = "";
while($i < 6) {
$pick = rand(0,$num);
for($j=0; $j <= count($id); $j++) {
if($j > 0 && $id[$j] == $pick) {
$in_set = 1;
break;
} else {
$id[] .= $pick;
$list .= $pick."<BR>";
$in_set = 0;
}
}
if($in_set == 0) {
$i++;
} else {
$i = $i;
}
}
That's it...Thanks,
Joe
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net