note 84340 added to function.rand

From: Date: Wed, 09 Jul 2008 21:45:00 +0000
Subject: note 84340 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-141333@lists.php.net to get a copy of this message
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; } } ---- Server IP: 69.147.83.197 Probable Submitter: 71.193.113.21 ---- Manual Page -- http://www.php.net/manual/en/function.rand.php Edit -- https://master.php.net/note/edit/84340 Del: integrated -- https://master.php.net/note/delete/84340/integrated Del: useless -- https://master.php.net/note/delete/84340/useless Del: bad code -- https://master.php.net/note/delete/84340/bad+code Del: spam -- https://master.php.net/note/delete/84340/spam Del: non-english -- https://master.php.net/note/delete/84340/non-english Del: in docs -- https://master.php.net/note/delete/84340/in+docs Del: other reasons-- https://master.php.net/note/delete/84340 Reject -- https://master.php.net/note/reject/84340 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141333) next »