Sub topic 1
Counting Orderings: Permutations and n!
Before you can judge whether a shuffle is fair, you need to know how many results it could produce. This chapter counts those results. Every later chapter relies on that number.
Prerequisites
This chapter assumes you are comfortable with:
- Arrays and zero-based indexing
- Loops and swapping two array elements
- Basic probability, including independent events and the uniform distribution
- Reading simple Python code
What a Permutation Is
A permutation is one specific ordering of a collection of distinct items. The list [A, B, C] has these six permutations:
ABC,ACBBAC,BCACAB,CBA
A shuffle takes a list and returns one of its permutations. Shuffling does not change which items are in the list. It only changes their order.
Counting by Filling Positions
To count permutations without listing them, fill the positions one at a time:
- The first position can hold any of the n items.
- The second position can hold any of the n − 1 items that remain.
- Each later position has one fewer option than the one before it, until only one item is left for the last position.
Every choice at one step can be combined with every choice at the next step, so you multiply the counts:
n × (n − 1) × (n − 2) × … × 1 = n!
This product is called "n factorial." By convention 0! = 1, because there is exactly one way to order an empty list.
The tree branches 3 ways at the first level, then 2, then 1. That gives 3 × 2 × 1 = 6 leaves, and each leaf is one permutation.
Checking the Count in Python
The standard library can list permutations and compute factorials, so you can confirm the count directly:
import itertools
import math
items = ["A", "B", "C"]
perms = list(itertools.permutations(items))
for p in perms:
print("".join(p))
print("listed:", len(perms), "factorial:", math.factorial(len(items)))
for n in (3, 10, 20, 52):
print(n, math.factorial(n))The script prints the six orderings, confirms that both counts are 6, and then shows how quickly n! grows.
How Fast n! Grows
| n | n! | Example |
|---|---|---|
| 3 | 6 | A tiny test list |
| 10 | 3,628,800 | A short playlist |
| 20 | 2,432,902,008,176,640,000 | About 2.4 × 10^18 |
| 52 | about 8.07 × 10^67 | A deck of cards |
Why This Number Matters for Shuffling
n! is the size of the target. A shuffle of n items is fair when it can produce every one of the n! permutations and each one is equally likely, so each comes up with probability 1/n!. The next chapter makes that definition precise. Later chapters use the same count to show three things: why some shortcut shuffles cannot be fair, why Fisher-Yates is fair, and why a random number generator with too little internal state cannot reach every ordering of a 52-card deck.
Exercise
How many permutations does a list of 5 distinct items have, and in how many of them is a particular item (say, X) in the first position?
Show answer
There are 5! = 120 permutations. If X is fixed in the first position, the other 4 items can be arranged in 4! = 24 ways. So X is first in 24 of the 120 permutations, which is 1/5 of them, as you would expect if every position is equally likely.
Exercise
A process makes three independent random choices: the first has 4 equally likely options, the second has 3, and the third has 2. How many equally likely outcomes does it have, and how does that number compare with the permutations of 4 items?
Show answer
It has 4 × 3 × 2 = 24 equally likely outcomes. The 4 items have 4! = 24 permutations, so the counts match. That match is what makes it possible for each outcome to correspond to exactly one permutation.