note 8996 deleted from function.shuffle by aidan
| From: | aidan@php.net | Date: | Thu, 26 Aug 2004 22:34:43 +0000 |
| Subject: | note 8996 deleted from function.shuffle by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75373@lists.php.net to get a copy of this message | ||
Note Submitter: hagen at von-eitzen dot de
----
Yes, shuffle doesn't shuffle well:
I made some experiments and found that shuffling a 10 row array would leave the first element in
place about three times more often than bringing the last element to the front!
But I'd suppose that the following is somewhat faster (don't forget to srand, of course!):
function my_shuffle(&$array) {
for ($j=count($array)-1; $j>0; $j--) {
if (($i = rand(0,$j))<$j) {$swp=$array[$i]; $array[$i]=$array[$j]; $array[$j]=$swp;}
}
}