note 25033 added to function.shuffle
| From: | php-general at lists dot php dot net | Date: | Sun, 08 Sep 2002 14:38:02 +0000 |
| Subject: | note 25033 added to function.shuffle | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-36344@lists.php.net to get a copy of this message | ||
And for the last optimized sample:
for($c = count($arr) - 1; $c > 0; $c--) {
$r = mt_rand(0, $c);
$v = $arr[$c];
$arr[$c] = $arr[$r];
$arr[$r] = $v;
}
You can stop when c == 0, instead of c < 0, as the element $arr[0] is the only remaining element
that has not been selected in the previous random selections, so $r in that last loop would always
be 0, and the element would only swap with itself.
--
http://www.php.net/manual/en/function.shuffle.php
http://master.php.net/manage/user-notes.php?action=edit+25033
http://master.php.net/manage/user-notes.php?action=delete+25033
http://master.php.net/manage/user-notes.php?action=reject+25033