Your shuffle algorithm is not very good.
It looks like a Fisher-Yates shuffle, which is what List::Util uses. Or did you spot a bug?
| [reply] |
| [reply] |
Yeah, that's Fisher-Yates (aka Knuth #12 or something). It works in place and I've never had any problems with it,* dunno why you think it's "not very good". Kind of a wacky place to put i-- but that's pretty trivial, style wise.
*(eg, just shuffled those 12 millon lines, no errors, since I am feeding that into something else I will notice if something gets duplicated et. al.) | [reply] |
You can trim your shuffle a little by omitting the line: next if $i==$j;.
Swapping an item with itself doesn't affect the algorithm's fairness, and doing it once costs less, than testing n times and avoiding it once.
And you save a little more by avoiding the list assignment:
my $tmp = $array[ $i ];
$array[ $i ] = $array[ $j ];
$array[ $j ] = $tmp;
Doesn't look as nice though.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
Including cleaning up the counter,
sub shuffle {
my $array = shift;
my $i = @$array;
while ($i) {
my $j = int rand($i--);
my $t = $array->[$i];
$array->[$i] = $array->[$j];
$array->[$j] = $t;
}
}
| [reply] [d/l] |
You can trim your shuffle a little by omitting the line: next if $i==$j;
Good point!
| [reply] |