Hash items into possible values and collisions become likely far sooner than intuition suggests, once passes roughly , not .
What it says
This formula answers one specific question. If you hash items into possible slots, uniformly and independently at random, what is the chance that at least two of them land on the same slot?
(1) Collision probability for n items over m slots.
It's the same question as the classic birthday paradox: put 23 people in a room () against 365 possible birthdays (). The odds two of them share a birthday are already better than even, even though 23 is nowhere near 365. Hashing falls into the same trap: a hash space that looks astronomically large can still produce collisions with a surprisingly small number of items.
Breaking it down
- : the probability that at least two of the items land on the same hash value.
- : the number of items being hashed.
- : the size of the hash space, the number of distinct possible output values (for example for a 32-bit hash).
It is easier to count the ways nothing goes wrong. The chance that all items land in distinct slots is a product of shrinking survival terms. The second item must avoid 1 slot, the third must avoid 2, and so on.
(2) Exact probability that every item lands in its own slot.
Where it comes from
Take logs of equation (2) and apply for small . The sum collapses to . That gives the exponential form in equation (1).
Seeing it work
Setting in equation (1) and solving for gives : even odds arrive at the square root of the output space, not the space itself.
A 32-bit hash has , about 4.3 billion possible values, so intuition says you would need billions of items before collisions become a real concern. The formula says otherwise: at items, the collision probability is already at 50%.
A 128-bit hash pushes up to , and the same formula puts the 50% mark at roughly quintillion items. That's why 128-bit hashes are treated as "collision-free" for essentially any practical dataset size.
Common misreadings
- 1
Assuming safety scales with m, not √m
A hash space of size does not buy you safe items: it buys you roughly . Doubling the hash width doesn't double the safe item count, it roughly squares it.
- 2
Trusting the formula more than the hash function
This approximation assumes a perfectly uniform, independent random hash. Real hash functions can fall well short of that: in Daniel Lemire's own experiment, 1,000 items reached a 100% collision rate with a hash function that theory predicted would need over 77,000 items to hit 50%. The formula is a best case, not a guarantee.
- 3
Applying it past n ≈ m
The approximation holds while . As approaches , use the exact product instead: the exponential approximation stops tracking it closely.