note 8996 modified in function.shuffle by irc-html

From: Date: Sat, 19 Jan 2002 22:10:45 +0000
Subject: note 8996 modified in function.shuffle by irc-html
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-25510@lists.php.net to get a copy of this message
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;} } } --was-- 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); $j>0; $j--) { if (($i = rand(0,$j))<$j) {$swp=$array[$i]; $array[$i]=$array[$j]; $array[$j]=$swp;} } } http://www.php.net/manual/en/function.shuffle.php

« previous php.notes (#25510) next »