Blog

Birthday Paradox: Estimating Random ID Collision Odds

With 23 people a shared birthday is more likely than not. Use the same math to estimate collision probability for random IDs, codes and UUIDs.

A graph with the number of people on the horizontal axis and the probability that two share a birthday on the vertical axis. The curve crosses the 50% line at 23 people and reaches 99% at 57.
Calculated assuming a 365-day year and birthdays spread evenly across it.
Contents

What are the chances that two students in a class of 40 share a birthday? A year has 365 days, so 40 people hardly seem enough for a match. Work it out, though, and the answer is about 89%: a class with no shared birthday is actually the unusual case. By 23 people the probability has already passed 50%.

This result is known as the birthday paradox. There is no logical contradiction in it; it earns the name because the answer is so far from what intuition expects. The same calculation applies well beyond birthdays. If you hand out booking numbers by picking random values from 000000 to 999999, then after issuing only 1,000 of them the chance that two numbers have already collided somewhere is close to 40%. Let’s check the birthday calculation first and then carry it over to random numbers and IDs.

You are not looking for someone who shares your birthday

Intuition goes wrong mainly because the question is easy to misread. Put yourself in a group of 23 and you will probably think “is there someone with the same birthday as me?” That probability is low. Each of the other 22 people was born on a different day from you with probability 364/365, so the chance that at least one of them shares your birthday is:

1 − (364/365)^22 ≈ 0.0586 (about 5.9%)

The actual question, however, is whether any two of the 23 share a birthday. There are 23 × 22 ÷ 2 = 253 ways to pick two people out of 23. Even if each pair matches with a probability of only 1/365, with 253 chances it would not be surprising for one of them to hit.

Only 22 of those pairs include you. The other 231 are pairs of people who have nothing to do with you. Because the number of pairs grows roughly with the square of the number of people, the probability climbs much faster than the headcount does.

Start from the probability that all birthdays differ

That said, you cannot simply add up the probabilities of the 253 pairs. If A and B share a birthday, and B and C do too, then A and C must share it as well; the pairs are not independent. So instead we first find the opposite, the probability that everyone’s birthday is different, and subtract it from 1.

The first person can have any birthday. The second differs from the first with probability 364/365. The third only has to avoid the two days already taken, so 363/365. In general, the n-th person must land on one of the remaining (365 − n + 1) days. The probability that everyone differs is the product of these, and 1 minus that product is the probability that at least one pair shares a birthday.

P(n) = 1 − (365/365) × (364/365) × (363/365) × … × ((365 − n + 1)/365)

Here are the results:

People Probability that two share a birthday
10 11.7%
20 41.1%
22 47.6%
23 50.7%
30 70.6%
40 89.1%
50 97.0%
57 99.0%
70 99.9%

At 22 people it is still just under half; 23 is the first group size where it passes 50%. With 57 people it reaches 99%. By contrast, for a better-than-even chance that someone shares your birthday, you need 253 other people. Both are questions about “matching birthdays”, yet changing the question changes the required group size enormously.

This calculation assumes a 365-day year (ignoring February 29) and that every day is equally likely as a birthday. Real birth dates are skewed by season and day of the week. But the more birthdays cluster on particular days, the easier it is for them to coincide, so any skew pushes the probability above the values in the table. Evenly spread birthdays are the case where a match is least likely.

I also ran a simulation that assigned random birthdays to 23 people, 100,000 times. A shared birthday appeared in 50,748 of the trials, or 50.7%, matching the calculated value.

Replacing 365 days with a million values: random 6-digit numbers

Wherever the birthday calculation says “365”, it really means the number of possible values. Swap in a different number and you can estimate collisions among randomly issued numbers.

Suppose you pick booking numbers or coupon codes at random from the six-digit range 000000 to 999999 and hand them out without checking whether each one has already been issued. There are a million possible values, so it is tempting to think a thousand or so will never clash. Yet by the time you have issued 1,000, the probability that two of them share a number is about 39.3%. At 1,178 it passes 50%. You have used only about 0.12% of the million values, and the odds of a collision are already even.

If N is the number of possible values and n is the number issued, the following approximation gives the collision probability quite accurately:

P ≈ 1 − exp(−n(n − 1) / (2N))

From this formula, the number of items at which the collision probability reaches 50% is about 1.177 × √N. Make N 100 times larger, and the count needed to reach 50% grows only tenfold. Here is how it works out for some typical formats:

How the ID is made Possible values Items for a 50% chance of a duplicate
4 digits 10,000 about 118
6 digits 1 million about 1,178
6 characters of lowercase letters and digits about 2.18 billion (36^6) about 55,000
8 characters of lowercase letters and digits about 2.8 trillion (36^8) about 1.98 million
UUID v4 2^122 (about 5.3 × 10^36) about 2.7 × 10^18

If randomly chosen numbers must never repeat, you need a mechanism that checks each new number against those already issued and picks again on a clash. The usual approach is to put a unique constraint on the number column in the database and, when an insert fails, generate a fresh random value and retry. It also pays to leave some headroom in the length, so you are not caught out if volumes grow beyond your estimates.

What we have covered so far is the probability that issued numbers collide with each other. Whether a code typed in at random by an outsider happens to match one of the issued codes is a different question. That one has the same shape as “does anyone share my birthday?”: with 1,000 six-digit numbers issued, a single guess hits with probability 1,000/1,000,000 = 0.1%. Preventing duplicates and making codes hard to guess call for different measures.

Why UUID v4 can be used without duplicate checks

UUID v4 fills 122 of its 128 bits with random data, giving 2^122, or about 5.3 × 10^36, possible values. Even after generating a billion (10^9) of them, the probability of any collision is only about 9.4 × 10^−20. Reaching 50% takes about 2.7 × 10^18 UUIDs; at a billion per second, that would take around 85 years.

This is why UUID v4 values generated independently on different servers and devices are often combined as they are, without relying on a unique-constraint check. The calculation does, however, assume that the random numbers are genuinely unpredictable. If the random number generator itself is flawed, for example by starting from the same seed, the 122 bits of space count for nothing. The UUID specification, RFC 9562, recommends using a cryptographically secure random number generator. Browsers’ crypto.randomUUID() and the UUID generator use this kind of randomness.

The differences between UUID v4, UUID v7 (which puts a timestamp at the front) and ULID are covered in Comparing UUID v4, UUID v7 and ULID. UUID v7 has a shorter random part, but in exchange only values generated within the same millisecond can collide.

The same reasoning applies to hash values. For the 256-bit SHA-256, a brute-force search for two different inputs that produce the same hash would take roughly 2^128 computations. That number is the square root of 2^256, and this estimate is known as the “birthday attack”. The steps for verifying downloaded files with SHA-256 are explained in How to use the Hash Generator.

Another famously counterintuitive probability is the Monty Hall problem. In both cases, writing out exactly what you are counting makes the answer much easier to accept.

Check the probabilities and simulation yourself (Python)

Save the code below as birthday.py and run it with python birthday.py. I tested it with Python 3.13.18, and it needs only the standard library. exact is the exact calculation that subtracts “everyone is different” from 1, approx is the approximation formula, and simulate assigns random birthdays or numbers and counts how often a duplicate occurs.

import math
import random


def exact(n, N=365):
    all_different = 1.0
    for k in range(n):
        all_different *= (N - k) / N
    return 1 - all_different


def approx(n, N):
    return -math.expm1(-n * (n - 1) / (2 * N))


def simulate(n, N, trials, seed=2026):
    rng = random.Random(seed)
    hits = 0
    for _ in range(trials):
        seen = set()
        for _ in range(n):
            value = rng.randrange(N)
            if value in seen:
                hits += 1
                break
            seen.add(value)
    return hits / trials


for n in (10, 20, 22, 23, 30, 40, 50, 57, 70):
    print(f"{n} people  {exact(n):.1%}")

print("Same birthday as you (23 people)", f"{1 - (364 / 365) ** 22:.2%}")
print("23 people, 100,000 trials", simulate(23, 365, 100_000))
print("1,000 six-digit numbers", f"{exact(1000, 10**6):.1%}")
print("1,178 six-digit numbers", f"{exact(1178, 10**6):.1%}")
print("1 billion UUID v4s", approx(10**9, 2**122))
print("UUID v4 count for 50%", f"{1.1774 * math.sqrt(2**122):.2e}")

The line for 23 people shows 50.7%, and the trial line shows 0.50748. The simulated result shifts slightly if you change seed, but the more trials you run, the closer it gets to the exact value.