View Single Post
  #2  
Old 10-24-2005, 03:30 PM
StevieG StevieG is offline
Senior Member
 
Join Date: Jan 2003
Location: Baltimore, MD, USA
Posts: 157
Default 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 />
Reply With Quote