note 116831 added to function.mt-rand
| From: | Flashbond | Date: | Fri, 06 Mar 2015 09:16:18 +0000 |
| Subject: | note 116831 added to function.mt-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201870@lists.php.net to get a copy of this message | ||
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.
?>
----
Server IP: 72.52.91.14
Probable Submitter: 94.54.97.136
----
Manual Page -- http://php.net/manual/en/function.mt-rand.php
Edit -- https://master.php.net/note/edit/116831
Del: integrated -- https://master.php.net/note/delete/116831/integrated
Del: useless -- https://master.php.net/note/delete/116831/useless
Del: bad code -- https://master.php.net/note/delete/116831/bad+code
Del: spam -- https://master.php.net/note/delete/116831/spam
Del: non-english -- https://master.php.net/note/delete/116831/non-english
Del: in docs -- https://master.php.net/note/delete/116831/in+docs
Del: other reasons-- https://master.php.net/note/delete/116831
Reject -- https://master.php.net/note/reject/116831
Search -- https://master.php.net/manage/user-notes.php