note 25033 deleted from function.shuffle by jimw
| From: | jimw@php.net | Date: | Sun, 08 Sep 2002 16:57:58 +0000 |
| Subject: | note 25033 deleted from function.shuffle by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-36352@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.