note 50015 added to function.rand

From: Date: Tue, 15 Feb 2005 15:29:56 +0000
Subject: note 50015 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-85071@lists.php.net to get a copy of this message
This is most simple random array generator, hope zo ;-) Moreover it uses some PHP array features :-), and does not search through array haystack, just checks for data if its set :P. <?php function GetRandomArray($min,$max,$num) { #data check $range = 1 + $max + $min; if ($num > $range) return false; if ($num < 1) return false; #data prep $result = array(); $a = rand($min,$max); $result[$a] = $a; #loop me baby ;-) while(count($result) < $num) { do {$a = rand($min,$max); } while(isset($result[$a])); $result[$a] = $a; } return $result; } var_dump(GetRandomArray(3,10,5)); ?> nice coding 4 U all :-) [k3oGH] ---- Manual Page -- http://www.php.net/manual/en/function.rand.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50015 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50015&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50015&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50015&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50015&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50015&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50015&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#85071) next »