note 84340 deleted from function.rand by danbrown

From: Date: Wed, 05 Nov 2008 15:36:02 +0000
Subject: note 84340 deleted from function.rand by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-146167@lists.php.net to get a copy of this message
Note Submitter: Prince Mabandla ---- A follow up on the simple class to generate a random (but unique) number. It obviously uses less memory when you specify a small range. class UniquelyRandom{ private static $alreadyGenerated = array(); public function urand($min = NULL, $max = NULL) { $range = ($min) && ($max) ? ($max - $min) + 1 : NULL; do{ $randValue = ($range) ? rand($min, $max) : rand(); $key = md5($randValue); if(count($rangeList) == $range) return NULL; if($range) $rangeList[$key] = NULL; }while(self::$alreadyGenerated[$key]); unset($rangeList); self::$alreadyGenerated[$key] = $randValue; return $randValue; } }

« previous php.notes (#146167) next »