(Please learn to use <code> tags around your code.)
The Fisher-Yates shuffle is usually written like this:
Update: No, actually it isn't as Dominus points
out below. See many other posts in this thread for the
correct implementation. I've left my version here as a
reminder to myself not to re-invent well-known algorithms
on he fly :(
foreach (0 .. $#array) {
my $i = rand @arr;
next if $i == $_;
@array[$_, $i] = @array[$i, $_];
}
--
<http://www.dave.org.uk>
"Perl makes the fun jobs fun
and the boring jobs bearable" - me
|