The collision paradox explained
Draw random values from a space of N possibilities and a repeat turns up far sooner than you would guess: not after N/2 draws, but after about the square root of N. Twenty-three people are enough for an even chance that two share a birthday, 119 random PIN codes for an even chance that two are the same, and 77,000 random 32-bit numbers for an even chance of a duplicate. The same square root decides how strong a hash function is against collisions and how many random IDs a system can hand out before two clash.
From birthdays to any set of values
The birthday problem asks how many people must be in a room before two of them probably share a birthday. The answer, 23, surprises nearly everyone, because we compare the people with ourselves instead of with each other: 23 people form 253 pairs, and each pair has a 1 in 365 chance of matching. Nothing in the argument is special to birthdays. Replace the 365 days by any N equally likely values (PIN codes, lottery tickets, the outputs of a hash function, randomly generated IDs) and the same reasoning tells you how soon a repeat, or collision, should appear. The puzzle is usually credited to Harold Davenport around 1927, who never published it because he could not believe it was new; the first version in print is Richard von Mises' paper of 1939 on the probabilities of filling boxes at random.
The product formula
Draw n values one after the other. The second avoids the first with probability 1 − 1/N, the third avoids both with probability 1 − 2/N, and so on, so the chance that all n are different is (1 − 1/N)(1 − 2/N) … (1 − (n − 1)/N) = N(N − 1) … (N − n + 1) / Nⁿ. The chance of at least one repeat is one minus that product. For 23 birthdays it is 50.7%, for 40 it is 89.1% and for 70 it is 99.9%. Because ln(1 − x) ≈ −x for small x, the product is close to e^(−n(n − 1)/2N), so P(repeat) ≈ 1 − e^(−n(n − 1)/2N). The exponent n(n − 1)/2N is the expected number of matching pairs: 253/365 ≈ 0.69 for 23 people. Once there are about as many pairs as values, a repeat is likely.
The square-root rule
Setting 1 − e^(−n²/2N) equal to ½ gives n ≈ √(2 ln 2 · N) ≈ 1.1774 √N draws for an even chance of a repeat. The rule is remarkably accurate: it predicts 22.5 for birthdays (the exact answer is 23), 117.7 for 10,000 PIN codes (exactly 119), 3,360 for the 8,145,060 tickets of a 6-of-45 lottery (exactly 3,361) and 77,163 for the 4,294,967,296 values of a 32-bit hash (exactly 77,164). Doubling the number of values multiplies the draws needed by only √2 ≈ 1.41, and squaring it merely doubles the number of digits. For a 1% chance the rule becomes n ≈ √(2N · 0.01005) ≈ 0.14 √N: about 9,300 random 32-bit numbers, or 610 million random 64-bit numbers.
Waiting for the first repeat
Turn the question round: keep drawing until a value comes up a second time, and count the draws. The chance that the wait exceeds n draws is exactly the chance of no repeat among n, so the curve P(repeat among n) is also the distribution of the wait. Its mean is 1 + Q(N), where Q is a function studied by Ramanujan and analysed by Knuth, and for large N it is close to √(πN/2) + 2/3 ≈ 1.2533 √N + 0.67: 24.6 draws for birthdays, 126 for PIN codes and 82,138 for 32-bit values. The wait divided by √N follows the Rayleigh distribution as N grows, the same shape as the distance of a random point in a two-dimensional bell curve from its centre. That is why the histogram of waits is skewed: most experiments end near √N, a few run three or four times as long.
Birthday attacks on hash functions
A cryptographic hash turns any file into a fingerprint of b bits, and a collision is two different files with the same fingerprint. Finding a file that matches one given fingerprint takes about 2ᵇ tries, but finding any two files that match each other takes only about 2^(b/2), by the square-root rule. Gideon Yuval described the attack in 1979: prepare many harmless variants of a contract that differ only in spacing or wording, and many variants of a fraudulent one, until one of each shares a hash; a signature on the first then also fits the second. So a 128-bit hash such as MD5 offers at most 64-bit collision security, and SHA-1, with 160 bits, at most 80 bits. Both fell faster than that through cryptanalysis: Wang Xiaoyun and her colleagues published MD5 collisions in 2004, forged certificates followed in 2008 and in the Flame malware of 2012, and Google and CWI Amsterdam announced the first SHA-1 collision, SHAttered, in 2017 after about 2⁶³ hash computations. Modern standards choose 256-bit hashes so that even the birthday bound, 2¹²⁸, is out of reach.
UUIDs and random IDs
A version 4 UUID has 122 random bits, N = 2¹²² ≈ 5.3 × 10³⁶. A billion UUIDs collide with a chance of about 9.4 × 10⁻²⁰, and an even chance of a duplicate needs about 2.7 × 10¹⁸ of them, which at a billion per second takes 86 years. Shorter IDs are a different story. Random 32-bit IDs are 50% likely to clash after 77,000 IDs and 1% likely after 9,300, so a system that hands out a few thousand of them a day will see a duplicate within weeks. A 64-bit random ID is safe for millions but not for trillions. With b random bits the chance of any clash among n IDs is about n²/2^(b + 1), so a one-in-a-million risk needs about 2 log₂ n + 19 bits: 79 bits for a billion IDs. When the space is smaller than that, a random ID needs a uniqueness check.
Passwords, PINs, lotteries and hash tables
If 10,000 people chose their PIN codes at random, 119 of them would already give an even chance of two identical codes. Real PINs are far from random, so repeats come much sooner: in one analysis of 3.4 million leaked four-digit codes, 1234 alone made up about 11%. The same holds for passwords: 26⁸ ≈ 2.1 × 10¹¹ lower-case passwords of eight letters need only 538,000 random choices for an even chance of a shared one, and people choose far less evenly, which is one reason password databases add a random salt so equal passwords do not get equal hashes. Lottery players who use quick picks share a combination surprisingly often: among 3,361 random 6-of-45 tickets two are probably identical. And in a hash table with a million buckets the first two keys land in the same bucket after about 1,254 keys on average, which is why every hash table needs a way to handle collisions.
Explore it
Open the collision paradox and choose a space under Values, from 365 birthdays to the 2¹²² values of a UUID, or type your own N, such as 2^40 or 1e12. Press Play: the first experiment draws values one by one until one repeats, the matching pair ringed in the grid and joined in the strip of draws, and then hundreds or thousands of experiments follow. On the chart the share of experiments that have hit a repeat traces the theory curve, and the histogram of waits peaks near √N. Move Draws to read the chance of a repeat for any number of draws and the expected number of matching pairs. Spaces whose expected wait is above 100,000 draws show the theory only. The pencil opens the picture in the photo editor with the grid, the draws, the collision, the curves, the markers and the labels on layers of their own.
Frequently asked questions
How many random draws give a 50% chance of a repeat?
About 1.1774 √N for N equally likely values: 23 for 365 birthdays, 119 for 10,000 PIN codes, 77,164 for 2³² values and about 5.1 billion for 2⁶⁴.
Why does a 128-bit hash give only 64 bits of collision security?
Because any two inputs with the same hash count as a collision, and by the birthday bound a repeat among random 128-bit outputs is likely after about 2⁶⁴ tries, the square root of 2¹²⁸.
Can two UUIDs ever be the same?
In principle yes, in practice no: random version 4 UUIDs have 122 random bits, so a billion of them collide with a chance of about 10⁻¹⁹, and an even chance needs about 2.7 × 10¹⁸ UUIDs. That holds only if the random number generator is good.
How long do you wait on average for the first repeat?
About √(πN/2) + 2/3 draws, roughly 1.25 √N: 24.6 for birthdays, 126 for PIN codes and 82,138 for 32-bit values.
Ready to try it?
Open PictorX in your browser — no install, no credit card required to start.
Open the collision paradox