Accurately computing collision risk is essential when architecting scalable distributed systems. This module provides mathematically exact formulas based on the generalized Birthday Problem.
collisionRisk(alphabetLength, idLength, generatedCount)
Calculates the probability ($0 \le P \le 1$) of at least one collision occurring when generating $N$ identifiers of length $L$ over an alphabet of size $A$ using the formula $P \approx 1 - e^{-\frac{N^2}{2 \cdot A^L}}$:
js
import { collisionRisk } from '@readytools/id';
// Risk of collision when generating 10,000,000 NanoIDs (len: 21, alphabet: 64)
const risk = collisionRisk(64, 21, 10000000);
// Returns: ~2.32e-14 (virtually zero)estimateIdLength(alphabetLength, expectedCount, maxRisk)
Computes the exact minimum character length required to keep collision probability below a maximum risk threshold:
js
import { estimateIdLength } from '@readytools/id';
// Calculate required length to generate 1 billion IDs with base62 alphabet at max 0.000001 (1 in a million) risk
const len = estimateIdLength(62, 1000000000, 0.000001);
// Returns: 16