note 76885 deleted from function.mt-rand by nlopess

From: Date: Wed, 08 Aug 2007 15:20:57 +0000
Subject: note 76885 deleted from function.mt-rand by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-129315@lists.php.net to get a copy of this message
Note Submitter: ales dot kladnik at bf dot uni-lj dot si ---- I tested the two functions that return multiple unique random numbers, which I've found between the comments for mt_rand() and rand(): - mrand($l,$h,$t,$len=false) - random_number_array($count, $from, $to) Here are the test results: http://www.bf.uni-lj.si/bi/mikroskopija/rand.php Clearly the better one is mrand(); <?php # Multiple Unique Random Numbers # # array mrand ( int min, int max, int count [, int strlen ] ) function mrand($l,$h,$t,$len=false){ if($l>$h){$a=$l;$b=$h;$h=$a;$l=$b;} if( (($h-$l)+1)<$t || $t<=0 )return false; $n = array(); if($len>0){ if(strlen($h)<$len && strlen($l)<$len)return false; if(strlen($h-1)<$len && strlen($l-1)<$len && $t>1)return false; do{ $x = rand($l,$h); if(!in_array($x,$n) && strlen($x) == $len)$n[] = $x; } while(count($n)<$t); }else{ do{ $x = mt_rand($l,$h); if(!in_array($x,$n))$n[] = $x; }while(count($n)<$t); } return $n; } ?>

« previous php.notes (#129315) next »