From: Reinder Verlinde Date: 2005-12-04T02:32:32+09:00 Subject: Re: need some Ruby magic In article , Jim Weirich wrote: > hammed wrote: > > I'd like to sort collections randomly. This is what I tried first: > > > > my_array.sort { |a,b| rand(2) } > > my_array.sort_by { rand } A quick test seems to indicate that this works with the ruby 1.8.2 implementation on my Mac. However, there is no guarantee that this call will select each of the n! permutations with equal probability. Whether it does will depend on the exact algorithm used by 'sort'. For instance, if sort uses the (inefficient for large arrays) bubble sort, the last key will end up staying there in half the cases. Reinder