note 116830 modified in function.mt-rand by sobak

From: Date: Mon, 09 Mar 2015 05:26:24 +0000
Subject: note 116830 modified in function.mt-rand by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-201893@lists.php.net to get a copy of this message
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. ?> --was-- A custom unique random generator class example: <?php class uniqueRandom { private $rands = array(); function generate($i, $ii) { while ($rand = mt_rand($i, $ii)) { 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 unique random numbers. ?> http://php.net/manual/en/function.mt-rand.php

« previous php.notes (#201893) next »