note 46713 added to function.shuffle
| From: | al at giicorp dot com | Date: | Thu, 21 Oct 2004 00:33:29 +0000 |
| Subject: | note 46713 added to function.shuffle | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-78862@lists.php.net to get a copy of this message | ||
When shuffling large sets this got a faster response, since $array is always the same size, those
extra millisecs counts.
function fisherYatesShuffle(&$array)
{
$total = count($array);// <--
for ($i = 0; $i<$total; $i++)
{
$j = @mt_rand(0, $i);
$temp = $array[$i];
$array[$i] = $array[$j];
$array[$j] = $temp;
}
}
----
Manual Page -- http://www.php.net/manual/en/function.shuffle.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46713
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46713&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46713&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46713&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46713&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46713&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46713&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46713&report=yes
Search -- http://master.php.net/manage/user-notes.php