RE: [PHP] Picking random numbers

From: Date: Tue, 21 Aug 2001 01:48:12 +0000
Subject: RE: [PHP] Picking random numbers
Groups: php.general 
Request: Send a blank email to php-general+get-63597@lists.php.net to get a copy of this message
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:seb@raceshoot.com] 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: 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

« previous php.general (#63597) next »