From: Neil Spring Date: 2004-12-16T02:05:42+09:00 Subject: Re: [ANN] rand.rb 0.9: Random access methods for Enumerables On Dec 14, 2004, at 7:24 PM, Ilmari Heikkinen wrote: > rand.rb is a library for picking random elements and shuffling. > This work is licensed under the same terms as Ruby itself. umm, could you maybe replace: def shuffle! sort!{rand <=> 0.5} end with: 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." -neil