On Mon, Jul 12, 2004 at 05:02:28PM +0900, gabriele renzi wrote:
> I don't understand: what happens if it returns the same float for two
> different numbers? they just get near in the sorting, what's the
> problem with that?

>> (1..10).sort_by{1}
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

When rand returns the same float twice for 2 numbers, they will keep
their relative ordering in the end result. The Mersenne Twister Ruby
uses has got a period of 2^19937-1, but Kernel.rand only returns 64
bits, so collisions are way more likely than it would seem at first
sight. For instance, for an array with 10 million elements, the
probability of being slightly biased is a bit over 2.71*10^-6. If you
use 128 bits (by doing sort_by{[rand,rand]}), the probability decreases
to 1.47*10^-25 (at that point it is more likely that your computer 
will explode or just crash).

For most intents and purposes, sort_by{rand} is unbiased enough.

-- 
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

'Ooohh.. "FreeBSD is faster over loopback, when compared to Linux
over the wire". Film at 11.'
	-- Linus Torvalds