note 84335 added to function.rand

From: Date: Wed, 09 Jul 2008 18:38:34 +0000
Subject: note 84335 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-141326@lists.php.net to get a copy of this message
A simple class to generate unique random values: class UniquelyRandom{ private static $alreadyGenerated = array(); public function urand($min = NULL, $max = NULL) { $range = ($min) && ($max) ? ($max - $min) + 1 : NULL; //inclusive $rangeList = array(); do{ $randValue = ($range) ? rand($min, $max) : rand(); $key = md5($randValue); if(count($rangeList) == $range) return 'All values between ' . $min . ' and ' . $max . ' have been exhausted'; if($range) $rangeList = array_merge($rangeList, array($key => $randValue)); }while(self::$alreadyGenerated[$key]); self::$alreadyGenerated[$key] = $randValue; return $randValue; } } $rand = new UniquelyRandom(); /* here is an example of how to use the code //Without a range $number = $rand->urand(); echo $number; //using a range $number = $rand->urand(3, 14); echo $number; */ ---- 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/84335 Del: integrated -- https://master.php.net/note/delete/84335/integrated Del: useless -- https://master.php.net/note/delete/84335/useless Del: bad code -- https://master.php.net/note/delete/84335/bad+code Del: spam -- https://master.php.net/note/delete/84335/spam Del: non-english -- https://master.php.net/note/delete/84335/non-english Del: in docs -- https://master.php.net/note/delete/84335/in+docs Del: other reasons-- https://master.php.net/note/delete/84335 Reject -- https://master.php.net/note/reject/84335 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141326) next »