Re: Random numbers

From: Date: Fri, 24 Nov 2000 09:43:51 +0000
Subject: Re: Random numbers
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-27064@lists.php.net to get a copy of this message
even easier would be just make no duplicates. <? $howmany = 6; $maxnum = 9; srand(time()); $rand[] = rand(0, $maxnum); for ($count = 0; $count < $howmany; $count++ ) { do { $tmp = rand(0, $maxnum); } while ( in_array($tmp, $rand) ); $rand[] = $tmp; } foreach($rand as $pos => $val) echo "a $val<br>\n"; ?> now there is one obvious thing to remeber here, never make howmany > maxnum you'll have a problem, you wont have enough non-duplicate numbers. haha. more info at http://www.php.net/manual/function.srand.php http://www.php.net/manual/function.rand.php Chris Lee Mediawaveonline.com In article <4.2.2.20001124172143.00a38100@redhotsweeps.com>, mail@redhotsweeps.com wrote: > Hello all, > > I need to generate 6 random numbers, 1-9, and make sure that there are > no more than 2 matches for each number. ie... > 1, 2, 3, 1,2,4 - good > 1, 3, 5, 8, 4, 3, 3 - not good > > Does anyone have or can anyone help me with this code? > > Thanks > > Chris > >

« previous php.general (#27064) next »