This means that permutations preserving the relative order of one (or
more) pair of elements of the original array are a bit more probable.

How often will that happen? The Mersenne Twister PRNG used by Ruby has
been proven to have a period of , so it
would seem at first sight that the answer is "not before the sun turns
into a nova". But Kernel#rand does not return the full state of the
PRNG: you only get 53 bits of pseudo-randomness out of a call to rand(). This
is becoming more interesting.

The birthday paradox

The birthday paradox states that given 23 people in a room, the odds are over
50% that two of them will have the same birthday.
It is not actually a paradox (there's no self-contradiction), it
merely defies common intuition.

It turns out that the problem with sort_by{ rand } is another instance of the
birthday paradox. In this case, we have a room with N people (N being the size
of the array to shuffle) and there are "days in a year".

Let's try to reproduce the 23-50% result, and apply the method to the other one.

There are

ways to assign a birthday to m people without any repetition. Therefore,
the probability that at least two people share a birthday is

BigDecimal to the rescue

Ruby's standard library includes an extension for arbitrary precision
floats, BigDecimal, whose very name suggests that we can think of it as
Bignum's cousin. BigDecimal objects have to be initialized with a String, but
that's not much of a problem

Quantifying the bias

I'd now like to just do something like

A(2**53, 1000000) / (2**53) ** 1000000

but it's obvious that won't fly. This is where a tiny bit of mathematical insight
proves much more useful than anything I could possibly find in the standard library.
I can find a cheap lower bound for the probability of repetition as follows:
there are possible pairs for the
N picks of rand() I'll do. For each of them, the probability that
the second has got the same value is . So the probability
of having at least one repetition is going to be higher than