Jason gave you a better way to do so, but if you want a "readable one", then
this code might work for you:

<?

function get_rand_array($from=0, $to=0, $keys=0) {
   $rand = Array();
   if(!$keys) Return $rand;

   if($keys>=($to-$from)) {
      Return $rand;
      // Doesn't make much sense, does it?
   }

   while(True) {
      $tmp_num = rand($from, $to);
      if($keys>sizeof($rand)) {
         if(!in_array($tmp_num, $rand)) 
            $rand[] = $tmp_num;
      }
      else
         Break;
   }

   Return $rand;
}


$random_array = get_rand_array(1, 200, 5);

echo '<PRE>';
print_r($random_array);
echo '</PRE>';

?>


regards,
Maxim Maletsky

www.PHPBeginner.com



-----Original Message-----
From: Seb Frost [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, August 21, 2001 10:13 AM
To: PHP General List
Subject: [PHP] Picking random numbers


OK I want to pick 5 random numbers from 1 to 200.  Easy.  They must all be
different.  Not so easy.

And I don't want to use shuffle because of the comments on php.net about it.

- seb


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to