note 76885 added to function.mt-rand
| From: | ales dot kladnik at bf dot uni-lj dot si | Date: | Fri, 03 Aug 2007 20:50:35 +0000 |
| Subject: | note 76885 added to function.mt-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129157@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Server IP: 213.250.2.28
Probable Submitter: 153.5.62.197
----
Manual Page -- http://www.php.net/manual/en/function.mt-rand.php
Edit -- https://master.php.net/note/edit/76885
Del: integrated -- https://master.php.net/note/delete/76885/integrated
Del: useless -- https://master.php.net/note/delete/76885/useless
Del: bad code -- https://master.php.net/note/delete/76885/bad+code
Del: spam -- https://master.php.net/note/delete/76885/spam
Del: non-english -- https://master.php.net/note/delete/76885/non-english
Del: in docs -- https://master.php.net/note/delete/76885/in+docs
Del: other reasons-- https://master.php.net/note/delete/76885
Reject -- https://master.php.net/note/reject/76885
Search -- https://master.php.net/manage/user-notes.php