RFC 4122 & RFC 9562 UUID Bit Allocation
A 128-bit UUID consists of 32 hexadecimal characters arranged in 5 hyphenated groups ($8-4-4-4-12$). Version 4 yields 122 bits of raw entropy after reserving version and variant flags.
1. UUID v4 Structure & Entropy Bits
Total Entropy=128 bits − 4 (version) − 2 (variant)=122 bits (2¹²² ≈ 5.3 × 10³⁶)
2. Birthday Paradox Collision Probability Approximation
P(collision)≈≈
n²2 × 2¹²²
n²1.06 × 10³⁷
Step-by-Step UUID v4 Construction Breakdown
Step 1: Draw 16 Cryptographic Random Bytes (128 bits)
Bytes: [0xf4, 0x7a, 0xc1, 0x0b, 0x58, 0xcc, 0x43, 0x72, 0xa5, 0x67, 0x0e, 0x02, 0xb2, 0xc3, 0xd4, 0x79]
Step 2: Inject Version 4 (`0x40`) & RFC 4122 Variant (`0x80`)
Byte 6 = (Byte[6] & 0x0f) | 0x40 → Version 4
Byte 8 = (Byte[8] & 0x3f) | 0x80 → Variant 10
Step 3: Hex Encoded Hyphenated String
Formatted Token=f47ac10b-58cc-4372-a567-0e02b2c3d479
UUID Version Comparison Matrix
| Version | Generation Basis | Chronological Sort | Optimal Use Case |
|---|---|---|---|
| UUID v4 | 122-bit CSPRNG Randomness | No (Random) | API keys, security tokens, web sessions |
| UUID v7 (New) | 48-bit Epoch ms + 74-bit Random | Yes (Sequential) | Database primary keys (B-Tree friendly) |
| UUID v1 | Timestamp + Node MAC Address | Partially | Legacy enterprise systems |
| UUID v5 | SHA-1 Namespace + Name Hash | No (Deterministic) | Deterministic idempotent resource mapping |