From: Joerg Plate Date: 2001-10-09T09:42:52+09:00 Subject: [ruby-talk:22268] Re: [Newbie] Switching from Perl : suffling This is a Fisher-Yates-Shuffle >> | sub shuffle { >> | srand; >> | my $array = shift; >> | for (my $i = @$array; --$i > 0; ) { >> | my $j = int rand ($i+1); >> | @$array[$i,$j] = @$array[$j,$i]; >> | } >> | } This is not the Fisher-Yates-Shuffle (and a biased algorithm according to the Perl Cookbook...) > class Array > def shuffle > each_index {|i| o = rand(size); self[i], self[o] = > self[o], self[i] } > end What about the following? class Array def shuffle last = self.size each_index { |i| j=last-i; k=rand(last)+1; self[i], self[o] = self[o], self[i] } end -- "I'm working on it." Jvyybj xvyyrq Onzov