My objective for producing a unique error code is for it to be truly unique without the need of keeping track of used codes. I have selected the javascript crypto.randomUUID() function for this purpose. It produces 5.3 undecillion possible values. My implementation of this is here and the exact algorithm is here in the codeGenerator.js file.
5.3 undecillion is the number 5.3 × 10³⁶, written out fully as:
5,300,000,000,000,000,000,000,000,000,000,000,000
When picking a million values, what is the probability that 2 will match?
- Pool size: P = 5.3 × 10³⁶
- Number of picks: N = 1,000,000 (10⁶)
Using the Birthday Problem approximation, which is explained here:
Probability (of at least one collision) ≈ 1 – e^(-N²/2P)
Calculation
- N² = (10⁶)² = 10¹²
- 2P = 2 × 5.3 × 10³⁶ = 1.06 × 10³⁷
- N²/2P = 10¹² / 1.06 × 10³⁷ ≈ 9.43 × 10⁻²⁶
Since that exponent is tiny, we can simplify:
Probability (collision) ≈ N²/2P ≈ 9.43 × 10⁻²⁶
≈ 0.0000000000000000000000000943
Or about a 1 in 10 quadrillion billion chance.
Conclusion
Using 1 million picks from a pool of 5.3 undecillion values, a collision is extraordinarily unlikely. You’d need to run this “pick 1 million values” experiment roughly 10²⁵ times before expecting even a single collision.
