note 69934 added to function.rand

From: Date: Wed, 27 Sep 2006 12:42:35 +0000
Subject: note 69934 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-117710@lists.php.net to get a copy of this message
frank, nick at nerdynick dot com, and kniht this is now O(n) instead of O(n^2) ish... <?php function rand_permute($size, $min, $max) { $retval = array(); //initialize an array of integers from $min to $max for($i = $min;$i <= $max;$i++) { $retval[$i] = $i; } //start with the the first index ($min). //randomly swap this number with any other number in the array. //this way we guarantee all numbers are permuted in the array, //and we assure no number is used more than once (technically reiterating prev line). //therefore we don't have to do the random checking each time we put something into the array. for($i=$min; $i < $size; $i++) { $tmp = $retval[$i]; $retval[$i] = $retval[$tmpkey = rand($min, $max)]; $retval[$tmpkey] = $tmp; } return array_slice($retval, 0, $size); } ?> ---- Server IP: 66.163.161.117 Probable Submitter: 65.125.76.61 ---- Manual Page -- http://www.php.net/manual/en/function.rand.php Edit -- https://master.php.net/note/edit/69934 Del: integrated -- https://master.php.net/note/delete/69934/integrated Del: useless -- https://master.php.net/note/delete/69934/useless Del: bad code -- https://master.php.net/note/delete/69934/bad+code Del: spam -- https://master.php.net/note/delete/69934/spam Del: non-english -- https://master.php.net/note/delete/69934/non-english Del: in docs -- https://master.php.net/note/delete/69934/in+docs Del: other reasons-- https://master.php.net/note/delete/69934 Reject -- https://master.php.net/note/reject/69934 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#117710) next »