From: Ilmari Heikkinen Date: 2004-12-16T03:31:08+09:00 Subject: Re: [ANN] rand.rb 0.9: Random access methods for Enumerables On 15.12.2004, at 19:05, Neil Spring wrote: > def shuffle! > each_index {|j| i = rand(size-j); self[j], self[j+i] = self[j+i], > self[j]} > self > end > > I would not rely on sort to do the right thing, and it is less > efficient than necessary. I use the second shuffle code extensively > in my code which deals with shuffling lots and lots of elements. > Presumably the second is O(n) where the sort based scheme is at best > O(n log n). A little googling suggests this is the "Fisher-Yates > shuffle." Thank you, changing #shuffle! to that and #shuffle to dup.shuffle! -Ilmari