note 43517 added to function.rand

From: Date: Thu, 24 Jun 2004 08:45:31 +0000
Subject: note 43517 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-71567@lists.php.net to get a copy of this message
This function returns an array of unique random numbers. <? // Multiple Unique Random Numbers // // Usage: mrand ( quanity of uniques, lowest value, highest value ) // function mrand($l,$h,$total){ $n = array(); do{ $num = rand($l,$h); if(array_search($num,$n) === false)$n[] = $num; }while(count($n)<$total); return $n; } // 20 unique numbers between 100 and 120 $numbers = mrand(100,120,20); // Sort the array sort($numbers); // Display the array foreach($numbers as $number) { echo $number.' '; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.rand.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+43517 Delete -- http://master.php.net/manage/user-notes.php?action=delete+43517&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+43517&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#71567) next »