Generate random UUID v4 values
A version 4 UUID is a 128-bit value in which 122 bits come from a cryptographically secure random source. The other 6 bits mark the version and the RFC variant, which is why every v4 UUID has a 4 at the start of its third group.
Worked example
- Random input bytes
- 9d 41 7c 02 e8 5b 13 f6 2a 7e c1 94 06 d3 b8 5f
- Result
- 9d417c02-e85b-43f6-aa7e-c19406d3b85f
| UUIDs generated | Chance of at least one duplicate |
|---|---|
| 10⁶ | ≈ 9.4 × 10⁻²⁶ |
| 10⁹ | ≈ 9.4 × 10⁻²⁰ |
| 10¹² | ≈ 9.4 × 10⁻¹⁴ |
| 10¹⁵ | ≈ 9.4 × 10⁻⁸ |
UUIDs needed for a 50% chance of a duplicate: ≈ 2.71 × 10¹⁸
How it works
- The generator fills 16 bytes with secure random data, then sets the high nibble of byte 6 to 4 (version) and the top two bits of byte 8 to 10 (variant). The third group therefore starts with 4 and the fourth group with 8, 9, a, or b.
- With 2¹²² possible values, the birthday approximation p ≈ 1 − e^(−n²/2¹²³) gives the chance that n random UUIDs contain at least one duplicate.
- A v4 UUID reveals no timestamp or hardware information. Treat it as an identifier, not as a password or authentication secret.
Generate 1-100 UUIDs at once
About UUIDs
UUID v1: Time-based UUIDs that include a timestamp and the MAC address of the computer generating the UUID. Useful when you need to know when the UUID was created.
UUID v4: Randomly generated UUIDs. This is the most commonly used version and provides good uniqueness with minimal information leakage.
UUID v5: Name-based UUIDs using SHA-1 hashing. Given the same namespace and name, you'll always get the same UUID. Useful for deterministic UUID generation.