The formula
Birthday approximation: p ≈ 1 − e^(−n(n − 1) ÷ 2N), where n = IDs generated and N = 2^bits possible IDs
Expected collisions ≈ n² ÷ 2N
IDs for a 50% chance ≈ √(2N ln 2) ≈ 1.1774 × √N
IDs for probability p (small) ≈ √(2Np)
UUID v4: 122 random bits; random string: bits = length × log₂(alphabet)
Worked example
- One billion UUID v4s (N = 2¹²² ≈ 5.3 × 10³⁶):
- n²/2N = (10⁹)² ÷ (2 × 5.3 × 10³⁶) ≈ 9.4 × 10⁻²⁰.
- So the chance of any duplicate is about 1 in 1.06 × 10¹⁹.
- You'd need about 2.7 × 10¹⁸ UUIDs for a 50% chance of one collision.
The birthday problem
In a room of just 23 people, there's a better than even chance that two share a birthday, even though there are 365 possible birthdays. That's because what matters is the number of pairs, which grows with the square of the number of people: 23 people make 253 pairs. Random identifiers behave the same way. Collisions become likely once the number of IDs approaches the square root of the number of possible IDs, so an ID space of 2^64 starts to see collisions after about 2^32, or four billion, IDs.
How safe are UUIDs?
A version 4 UUID has 122 random bits, giving about 5.3 × 10^36 possible values. Even generating a billion UUIDs per second for a century, about 3 × 10^18 IDs, gives roughly a 50% chance of a single duplicate. For almost every real application the risk is negligible, far smaller than the chance of hardware errors or bugs. The real-world collisions that do happen almost always come from poor random number generators, such as a virtual machine cloned with the same random state, rather than from the mathematics.
UUID v7 and time-based IDs
UUID version 7, standardised in RFC 9562 in 2024, starts with a 48-bit millisecond timestamp followed by random bits, so IDs sort by creation time, which makes database indexes far more efficient. Collisions can only happen between IDs created in the same millisecond, so the relevant count is IDs per millisecond against its 74 random bits. Choose the v7 option and enter the number of IDs your busiest system creates in a single millisecond to check the risk.
Short IDs and custom alphabets
Short random strings are popular for URLs, invite codes and order numbers, but they collide much sooner. An 8-character code from 62 letters and digits has about 47.6 bits, so collisions become likely after about 17 million codes, and even a million codes carries a small chance. Nanoid's default 21-character IDs from a 64-symbol alphabet have 126 bits, similar to UUIDs. Use the custom option to test your own format, and always enforce uniqueness in the database anyway, retrying on the rare conflict.
Generation rate
If you know how fast you'll create IDs rather than how many, choose generation rate and enter IDs per second and the number of years. The calculator multiplies them out. 1,000 IDs per second for 10 years is about 315 billion IDs, which is completely safe with UUID v4 but would make collisions virtually certain with 64-bit random integers, at about 2,700 expected duplicates. The 50% and one-in-a-billion figures give you headroom limits for planning.
Putting tiny probabilities in perspective
Probabilities such as 1 in 10^19 are hard to picture. For comparison, the chance of being struck by lightning in a given year is roughly 1 in a million, and the chance of a cosmic ray flipping a bit in a computer's memory over a year is far higher than a UUID v4 collision for any realistic workload. Engineering effort is better spent on correct database constraints, good random number generators and backups than on worrying about UUID uniqueness.
Related tools
The same counting underlies password strength: our password entropy calculator uses bits of randomness to estimate crack times. For exact counts of arrangements and selections, see the permutation and combination calculator, and for the chance of winning games of luck, the lottery odds calculator and gacha drop probability calculator.