From: seebs@... (Peter Seebach) Date: 2007-04-30T11:28:35+09:00 Subject: Re: sorting by rand? In message <40D2708E-0D0C-42E4-82B3-E31D93481880@mac.com>, Gary Wright writes: > >On Apr 29, 2007, at 10:01 PM, Peter Seebach wrote: >> So, two vaguely related questions: >> 1. Is there a reasonably sane way to do this, should I ever find >> myself wanting one? > >Short answer: array.sort_by { rand } > >Longer answer: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby- >talk/168963 Well, okay. It works in practice. Is it guaranteed to work, and is it actually reasonable to expect the output to be randomly shuffled? Just for comparison, I tried: l = a.length a.each_index { |x| a[x] = a[rand(l)] } I'm pretty sure I got the generic shuffle algorithm right (Knuth FTW), and if so, it produces quality random results in about 1/4 the time. -s