From: Kero Date: 2005-12-05T05:32:33+09:00 Subject: Re: need some Ruby magic > How about just doing this? > > class Array > def shuffle > newarr = self > self.length.times do |x| > y = rand(self.length) > newarr[x], newarr[y] = newarr[y], newarr[x] > end > newarr > end > end > > shuffled_array = my_array.shuffle "Just" doing that 1) changes the receiver (use new_arr = self.dup instead) 2) definitely has a bias. Look e.g. at an array of length three. You will first swap the first element with 3 possibilities, then the second and so on; you're going through 3**3 possibilities. There are 3! possible permutations, but alas, 3! == 6 is not a perfect divisor of 3**3 == 27, thus you have a bias.