Computer Science

Collision Probability Calculator

Estimate birthday-bound collision probability for hashes, IDs, UUID-like spaces, and random tokens.

Collision Probability

9.404e-24%

No-Collision Probability

100%

Space Size

1e36.73

Birthday 50% Point

1e18.43

Random IDs and the Birthday Problem

Pairs Create the Birthday Effect

Collision probability is the quiet risk behind hashes, random IDs, invite codes, filenames, database keys, and tokens. If the space is large enough, collisions are practically impossible. If the space is smaller than it looks, collisions arrive sooner than intuition expects. The birthday problem explains why: you are not only comparing each new value with one fixed value, you are comparing every generated value with every other generated value. The number of pairs grows quickly.

A random identifier space with b bits has 2^b possible values. Generating one value is unlikely to hit a specific other value. Generating many values creates many chances for any two to match. Around the square root of the space size, collision probability becomes noticeable. That is why a 32-bit random ID is not safe for large datasets, while a 122-bit UUIDv4-style space is enormous for ordinary application counts. The bits matter more than the visual length of the string.

Generated items should be the total count that can collide in the same namespace. If IDs are only unique per customer, use the largest customer's count. If all customers share one table, use the whole table. Random bits should be the actual entropy after fixed prefixes, version bits, encoding choices, truncation, and formatting are removed. A 32-character string is not automatically 128 random bits. It depends on alphabet size and how the generator samples from it.

One Million Random Identifiers

The working equation is P(collision) ~= 1 - exp(-n*(n-1)/(2*2^bits)).

The birthday approximation is one minus exp of negative n times n minus one divided by twice the space size. For small probabilities, n squared over twice the space is a useful shortcut. If you generate one million 64-bit values, the probability is roughly 1e12 divided by 2 times 2^64, or about 2.7e-8. That is small. With 32 bits, the same million values almost certainly collide. The calculator handles the exponential form so larger values remain readable.

Model limit: Assumes uniformly random independent values. Biased generators or truncated IDs can collide much sooner.

Randomness Quality Still Governs

A version-4 UUID has 122 random bits after fixed fields. For one million independently generated IDs, the birthday approximation uses x = n(n−1)/(2×2^122), about 9.40×10^-26. When x is tiny, collision probability is approximately x, or 9.40×10^-24 percent. The 50 percent collision point is near √(2 ln 2 × 2^122), roughly 2.7×10^18 generated IDs. That does not mean the first collision occurs exactly there; it describes probability across repeated experiments.

Reducing the random space to 32 bits changes the conclusion. With 100,000 IDs, x is about 1.16 and collision probability is roughly 69 percent. A database uniqueness constraint should therefore remain in place even when the theoretical risk is small, and generation code must handle a rejected duplicate. Biased or repeated random seeds invalidate the uniform model completely. Monitor generator failures and avoid truncating identifiers in logs, exports, or user interfaces where the shortened form might be mistaken for the actual key.

Turning Probability into an Engineering Decision

The biggest mistake is treating a hash length, UUID text length, or database column width as entropy. Fixed bits, timestamps, counters, sharding prefixes, and biased generators reduce the random space. Truncating hashes for convenience can be fine, but the collision math must be redone after truncation. Another mistake is ignoring retry behavior. If the system checks for collisions and regenerates, collision probability becomes an operational cost rather than immediate data loss, but a small space can still cause slow inserts or hot loops.

Collision probability should be interpreted against the consequence of a collision. A temporary cache key can tolerate more risk than a public reset token or a permanent primary key. The 50 percent birthday point is useful because it shows the scale where the space becomes crowded. You usually want to operate far below that point. If the calculated risk is uncomfortable, add random bits, partition the namespace intentionally, check uniqueness at write time, or use deterministic IDs where appropriate.

Use this calculator when deciding how many characters to keep from a hash, how long random invite codes should be, whether a UUID-like value is enough, or how many test fixtures can be generated safely. In code review, ask where the random bits come from and whether the uniqueness boundary is clear. For security tokens, collision is only one concern; unpredictability and secret handling matter too. A token can avoid collisions and still be unsafe if it is guessable or logged.

A good ID design note records alphabet, random bits, generated count, namespace boundary, collision probability, and collision handling. The calculator gives the probability, but engineering judgment sets the acceptable risk. Small systems often get away with short IDs until they grow, merge namespaces, or expose IDs publicly. It is better to spend a few extra characters early than to migrate a crowded keyspace later. Randomness is cheap when the format is chosen before users and data depend on it.

Continue exploring