note 81995 added to function.mt-rand

From: Date: Sun, 23 Mar 2008 07:02:37 +0000
Subject: note 81995 added to function.mt-rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-137341@lists.php.net to get a copy of this message
Here's a simple function i've made for generateing unique random numbers. Be carefull, the function will go into an infinite loop if you request more unique numbers then the given range (ex. if you call unic(1, 5) five times it's ok, but the 6'th time it will go into an infinite loop). $unic_arr = array(); //the array in witch the used numbers are stored. function unic($min, $max) { global $unic_arr; $rand = mt_rand($min, $max); $tf = 1; while(true) { if(!in_array($rand, $unic_arr)) { $tf = 0; break; } else $rand = mt_rand($min, $max); } array_push($unic_arr, $rand); return $rand; } //to call the function use: unic($min, $max); //ex. unic(1000, 9999); ---- Server IP: 69.147.83.197 Probable Submitter: 78.96.39.99 ---- Manual Page -- http://www.php.net/manual/en/function.mt-rand.php Edit -- https://master.php.net/note/edit/81995 Del: integrated -- https://master.php.net/note/delete/81995/integrated Del: useless -- https://master.php.net/note/delete/81995/useless Del: bad code -- https://master.php.net/note/delete/81995/bad+code Del: spam -- https://master.php.net/note/delete/81995/spam Del: non-english -- https://master.php.net/note/delete/81995/non-english Del: in docs -- https://master.php.net/note/delete/81995/in+docs Del: other reasons-- https://master.php.net/note/delete/81995 Reject -- https://master.php.net/note/reject/81995 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#137341) next »