# Random Shuffles

Here is a suprising claim. If you shuffle a standard deck of cards seven times, with almost total certainty you can claim that the exact ordering of cards has never been seen! Wow! Let's explore. We can ask this question formally as: What is the probability that in the $n$ shuffles seen since the start of time, yours is unique?

### Orderings of 52 Cards

Our adventure starts with a simple observation: there are **very** many ways to order 52 cards. But exactly how many unique orderings of a standard deck are there?

That is over $8 \cdot 10^{67}$. Recall it is estimated that there are around $10^{82}$ atoms in the observable universe

### Number of Shuffles Ever Seen

Of course we don't know what the value of $n$ is — nobody has been counting how many times humans have shuffled cards. We can come up with a reasonable overestimate. Assume $k$ = 7 billion people have been shuffling cards once a second since cards were invented. Playing cards may have been invented as far back as the Tang dynasty in the 9th century. To the best of my knowledge the oldest set of 52 cards is the Topkapı deck of cards in Istanbul around the 15th century ad. That is about $s$ = 16,472,828,422 seconds ago. As such our overestimate is $n = s \cdot k \approx 10^{20}$.

Next let's calculate the probability that none of those $n$ historical shuffles matches your particular ordering of 52 cards. There are two valid approaches: using equally likely outcomes, and using independence.

### Equally Likely Outcomes

One way to the probability that your ordering of 52 cards is unique in history is to use Equally Likely Outomces. Consider the sample space of all the possible ordering of all the cards ever dealt. Each outcome in this set will have $n$ card decks each with their own ordering. As such the size of the sample space is $|S| = (52!)^{n}$. Note that all outcomes in the sample space are equally likely — we can convince ourselves of this by symmetry — no ordering is more likely than any other. Out of that sample space we want to count the number of outcomes where none of the orderings matches yours. There are $52! - 1$ ways to order 52 cards that are not yours. We can construct the event space by steps: for each of the $n$ shuffles in history select any one of those $52! - 1$ orderings. Thus $|E| = (52!-1)^n$.

So in English, the probability that someone has ever seen your cards is less than 0.0 followed by 100 zeros before a 1. It is safe to say your ordering is unique.

In python, you can use a special library called decimal to compute very small probabilities. Here is an example of how to compute $\log \frac{52!-1}{52!}$:

```
from decimal import *
import math
n = math.pow(10, 20)
card_perms = math.factorial(52)
denominator = card_perms
numerator = card_perms - 1
# decimal library because these are tiny numbers
log_numer = Decimal(math.log(numerator))
log_denom = Decimal(math.log(denominator))
log_pr = log_numer - log_denom
# 0E-45
print(log_pr)
```

### Independence

Another approach is to define events $D_i$ that the $i$th card shuffle is different than yours. Because we assume each shuffle is independent, then $P(U) = \prod_i P(D_i)$. What is the probability of $(D_i)$? If you think of the sample space of $D_i$, it is 52! ways of ordering a deck cards. The event space is the 52! - 1 outcomes which are not your ordering.

### How Random is your Shuffle?

A final question we can look into. How do you get a truly random ordering of cards? Dave Bayer and Persi Diaconis in 1992 worked through this problem and published their results in the article Trailing the Dovetail Shuffle to its Lair. They showed that if you shuffle a deck of cards seven times using a riffle shuffle also known as the dovetail shuffle, you are almost garunteed a random ordering of cards. The methodology used paved the way for studying psuedo random numbers produced by computers.