Previously on this blog, I have talked about Buffon’s Needle, which is a way to measure using random numbers. In this entry, I will be talking about a way to measure another fundamental constant , also by using random numbers.
To measure , we will shuffle a deck of cards many times and count the number of shuffles that result in the deck being deranged. This means that no card remains in its original position in the deck. This type of arrangement is called a “derangement”.
We will start by computing the theoretical probability () getting a derangement, which turns out to be or about 36.7%, for a sufficiently large number of cards. We will then experimentally measure by shuffling cards.
To compute the theoretical probability , we need to understand two numbers:
The first is the total number of arrangements that the cards can be in ().
The second is how many of those arrangements are derangements ().
The theoretical probability is the ratio of those two numbers:
and it approaches .
(Or you could say approaches .)
The first number is easy, it’s just the factorial of the number of cards in the deck ().
The second number is not as straightforward. We have to count the number of derangements a deck can have.
To show a simple example of counting derangements, let’s look at a deck of only 3 cards:
There are 6 ways to arrange these 3 cards ()
(this one is a derangement)
(this one is a derangement)
Only 2 of these arrangements have every card moved from its original position. So the number of derangements for 3 cards is 2. So in this case, the probability of getting a derangement is about 33%.
That’s close to , but not quite, because it turns out 3 cards are just not enough to produce that probability. We need more.
In general, with a deck of cards, we can arrange any number of those cards in number of ways, where:
We can use this formula to count the number of ways there are to rearrange a subset of the cards, while cards remain in a fixed position (we are counting the number of permutations).
Note that , then by definition, the corresponding arrangements will not be derangements. If we can count all of these, then we can subtract this number of arrangements from the total number to get the number of derangements.
Let’s see this in action by going back to the deck of 3 cards. If we select all the cards to rearrange, we can get all 6 possible permutations:
If we select only 2 cards out of the 6 to rearrange, we only get a subset of the arrangements. However if we count how many there are with the formula we get 6 permutations:
So there are 6 ways to arrange 2 cards (6 ways to hold 1 card fixed). But why do we get 6 permutations here? Well, let’s look at all the ways to do this.
If we select 2 and 3 to rearrange, we get 2 permutations:
If we select A and 3 to rearrange, we get 2 permutations:
And if select A and 2 to rearrange, we get 2 permutations:
Notice however that occurs 3 times, and so there are actually 4 unique arrangements here, and they are not derangements by definition. So we have derangements for 6 cards.
But is there a way to count unique arrangements without doing it by hand like we did here? Let’s keep going with this pattern.
If we select only 1 card to rearrange, then that’s trivial, because you can’t rearrange one card.
If selecting A:
If selecting 2:
If selecting 3:
All 3 permutations are the same arrangement!
Then if we select no cards to rearrange, this is also trivial.
If selecting nothing:
That’s just 1 permutation.
All duplicate permutations, are accounted for here. The number of permutations for accounts for one of the duplicates in :
The permutations left in this difference account for 2 of the duplicates in :
And you are left with the unique 4 arrangements that are not derangements.
So to get the number of derangements, we just have to subtract this from the total number of arrangements :
And so we see that
We have a pattern here. We can see that by distributing the negative signs in the above equation we get:
This can be expressed as a sum:
Or more explicitly:
It turns out that this pattern holds for decks of any number of cards (just replace the 3 here):
For a deck of 3 cards, the number of derangements is:
For a deck of 4 cards:
For a deck of 5 cards:
which then give us the probabilities of derangement:
So by 4 cards, we start seeing a probability closer to .
Now, to get the probability of getting a derangement for cards:
The factors cancel out, and we are left with:
Since the order of addition doesn’t matter, we can change the direction of this sum to start at and end at 0:
If we do some relabeling, the relationship to will become more obvious. Let’s set , so we can write:
This looks like a truncated Taylor expansion of the exponential function .
A Taylor expansion is a way of expressing a function as a polynomial.
The Taylor expansion of the exponential function is:
If we plug in we get:
which is the limiting case of a deck with infinite cards:
This equivalence to the Taylor expansion is the reason why as you increase the number of cards, the probability of getting a derangement approaches .
To put it another way:
Now that we have seen the theoretical probability, let’s do the experiment.
Take a standard 52 card deck, note the original positions of the cards, then shuffle and see if you have a derangement. Keep shuffling and noting if the result is a derangement. Keep doing this about 40,000 times. Count the total number of shuffles and the number of derangements you get.
If you then take the ratio you will get something approaching .
Just kidding. That would be extremely tedious. Instead, we can have a computer it for us.
For a deck of 52 cards, it seems to take about 40k shuffles for the ratio of shuffles to derangements to approach . See the figure above.
The code used to compute from shuffles is shown below:
Here I used MATLAB code, to take advantage of vectorization and logical array operations. The inputs are cards (the number of cards in a deck) and games (the number of shuffles). Wins (a win is when you get a derangement) are then counted.
function eulersnum = eulerdeck(cards,games) wins = 0; series = 1:cards; for ii=1:games shuffled = randperm(cards); wins = wins + ~any(series == shuffled); end eulersnum = games/wins; end
The known value of is 2.7183…
To get an accurate measurement we should use a deck with a lot of cards, since that will include more terms of the Taylor expansion. We should also shuffle many times to be sure that we capture the probability.
So with a deck of 1000 cards, and 10 trials, each of 1000 shuffles, we measure to be: