Re: Card Shuffling Algorythm
From the Perl FAQ on data manipulation, here is the Fisher Yates shuffle, which will shuffle an array in linear time.
This is in Perl, and its randomness is dependent on the rand function.
<font class="small">Code:</font><hr /><pre>
sub fisher_yates_shuffle {
my $deck = shift; # $deck is a reference to an array
my $i = @$deck;
while ($i--) {
my $j = int rand ($i+1);
@$deck[$i,$j] = @$deck[$j,$i];
}
}
</pre><hr />
|