note 116831 deleted from function.mt-rand by sobak

From: Date: Sat, 07 Mar 2015 07:50:55 +0000
Subject: note 116831 deleted from function.mt-rand by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-201878@lists.php.net to get a copy of this message
Note Submitter: Flashbond ---- My previous custom unique random generator class example with a safety valve: <?php class uniqueRandom { private $rands = array(); function generate($i, $ii) { if (array_diff(range($i, $ii), $this -> rands)) { while ($rand = mt_rand($i, $ii)) { if (!in_array($rand, $this -> rands)) { $this-> rands [] = $rand; return $rand; } } } return "No more unique number to return."; } } //Test example $myUnique = new uniqueRandom; for ($i = 0; $i < 5; $i++) { echo $myUnique -> generate(1, 10)." "; } //This will produce 5 unique random numbers from 1 to 10. ?>

« previous php.notes (#201878) next »