note 116830 deleted from function.mt-rand by cmb
| From: | cmb@php.net | Date: | Sun, 31 Mar 2019 08:37:04 +0000 |
| Subject: | note 116830 deleted from function.mt-rand by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210463@lists.php.net to get a copy of this message | ||
Note Submitter: Flashbond
----
A custom unique random generator class example:
<?php
class uniqueRandom {
private $rands = array();
function generate($min, $max) {
while ($rand = mt_rand($min, $max) && array_diff(range($min, $max), $this ->
rands)) {
if (!in_array($rand, $this -> rands)) {
$this-> rands [] = $rand;
return $rand;
}
}
}
}
//Test
$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.
?>