From: Dan Zwell Date: 2007-04-30T16:24:17+09:00 Subject: Re: sorting by rand? Peter Seebach wrote: >>> It seems expensive. > >> That's almost exactly what sort_by{rand} does... > > Right. > > And so far as I can tell, sort_by{rand} is inefficient compared to > a canonical shuffle algorithm. That's true, but keep in mind that sort_by{rand} is code that's built into the interpreter, so it's still gonna be pretty fast. I tested this on my system a few weeks ago, and rerunning the benchmark program, the traditional sort only becomes faster when the arrays in question have over 5,000 elements. After all, it's a better algorithm, but it's written in ruby and not C. > I also can't see an immediate reason to do the random numbers sort more > than once; I guess to reduce the chances of collisions preserving order. If the numbers that rand() generates are good enough, the chance of collisions preserving order should be the same as the chance of the next iteration of randomizing putting them back in the same order, shouldn't it? Dan