From: Florian Gross Date: 2004-07-12T10:02:26+09:00 Subject: Re: Ruby quickies and useful idioms Mikael Brockman wrote: > gabriele renzi writes: >>> [some shuffling stuff] >> the best shuffle I can think of is the Grossian shuffle (quoting >> Flroain Gross (actually written with a different character than ss): It's actually "Florian Gro�", but I've seen the last character come out as an y with two dots above it which is why I usually use "Gross" where I can't be sure about it coming out right. ;) Oh, and thanks for naming this after me. :) >> class Array >> def shuffle >> sort_by {rand} >> end >> end >> You may even found extensions.rubyforge.org interesting > It's very nifty indeed. I heard it was biased in some obscure way, > though. It's biased when rand returns the same float for two or more elements of the Array. This means that it will be ~0.012% biased on an Array that has one million entries and ~0.0001% biased on an Array with 100000 entries when you're on a 32-bit machine. On 64-bit machines you will have a 50% chance of one element being biased for an Array with 5,000,000,000 elements. If that's still to likely for you you can use sort_by { Array.new(100) { rand }} instead. The problem with all this is that people frequently confuse sort_by { rand } with sort { rand } and variants of it. Oh, and it might even make sense to add it to Enumerable instead of Arrays. After all #sort_by is defined for all Enumerables. Regards, Florian Gross