From: Dan Zwell Date: 2007-08-27T07:14:42+09:00 Subject: Re: sort_by { rand } not working Lionel Bouton wrote: > Dan Zwell wrote: >> for i in 0...@all.length >> j = i+rand(@all.length-i) >> @all[i], @all[j] = @all[j], @all[i] >> end >> >> The second one will be faster, but you probably shouldn't care on >> small arrays. >> > > You could even optimize this further with 0...@all.length-1 instead of > 0...@all.length (the last loop is a noop), but I agree, this is too much > pain for the eyes to bother unless it becomes a real performance problem. > > Sorry if this has already been said, in fact I didn't even remember this > code on the mailing-list at all (too much traffic for me maybe). > > Lionel > > Heh, you're right about the noop, and that hasn't been said. This code was never on the list in this form--those 4 lines are a condensation of something I posted to this list from my algorithms book, and someone's suggestion that optimized it further (use parallel assignment instead of making a swap() function). Dan