UUID v4 Explained: Randomness, Collisions & Cryptography
Explore the mathematics behind UUID Version 4 randomness. Learn why collision odds are negligible (1 in 2.71 quintillion) and how CSPRNG generates secure UUIDs.
The Mathematics of UUID v4 Entropy
A Version 4 UUID reserves 6 bits for metadata (4 bits for the version and 2 bits for the variant), leaving 122 bits of pure random entropy. The total number of possible UUID v4 values is:
2^122 = 5,316,911,983,139,663,491,615,158,242,912,234,496 (~5.3 × 10^36)
Collision Probability & Birthday Paradox
Even if you generated 1 billion UUIDs per second for 100 years, the probability of creating a single collision remains less than 50%. In real-world application databases generating millions of records daily, the collision risk is practically zero.
Generating UUID v4 in Modern JavaScript
// Native browser and Node.js 16+
const id = crypto.randomUUID();
console.log(id); // "c9a646d3-9c61-4cd7-893e-8b47f32f3f98"