randomize an array in perl
July 16th, 2007 by Lawrence David
here’s one legitimate way to randomize an array in perl:
# fisher_yates_shuffle( \@array )
sub fisher_yates_shuffle {
my $array = shift;
my $i;
for ($i = @$array; –$i; ) {
my $j = int rand ($i+1);
next if $i == $j;
@$array[$i,$j] = @$array[$j,$i];
}
}
thanks perl cookbook!