UUID v4 generator
A UUID v4 is 122 random bits written as 36 characters in the form 8-4-4-4-12. The version nibble is 4 and the variant nibble is 8, 9, a or b. It is unique enough to generate without coordination, but it is not a secret and it does not sort by time.
You need an identifier that will not collide with one generated on another machine, by another process, at the same moment. That is exactly the problem UUID solves, and it solves it without a database round trip.
The anatomy
f47ac10b-58cc-4372-a567-0e02b2c3d479
└── 32 hex digits ──┘ │ │
│ └─ variant: 8, 9, a or b
└────── version: 4
- 128 bits total, of which 122 are random.
- 6 bits are fixed: 4 for the version and 2 for the variant. That is why a v4 UUID always has a
4as the first character of the third group. - 36 characters with hyphens, 32 without.
Are collisions a real risk?
122 random bits gives about 5.3 × 1036 possible values. Generating a billion UUIDs a second for a century would still leave the probability of any collision negligible. The risk in practice is not randomness — it is a weak random source. A generator seeded from the clock, or one that reuses a seed across forked processes, can produce duplicates. Use crypto.randomUUID() or an equivalent CSPRNG-backed source.
Why v7 exists
v4 values are random, so they sort into a random order. That is bad for database indexes: inserting random keys scatters writes across the B-tree, and range queries return nothing meaningful. UUID v7 puts a millisecond timestamp in the leading bits, so values sort roughly by creation time while keeping enough randomness to stay unique. For a new table with UUID primary keys, v7 is usually the better default.
| Version | Source | Sortable |
|---|---|---|
| v4 | Random | No |
| v5 | SHA-1 of a namespace + name | No, but deterministic |
| v7 | Timestamp + random | Yes, by creation time |
Not a secret
A UUID is an identifier, not a token. It carries no entropy guarantee beyond uniqueness and is often exposed in URLs, logs and exports. An "unguessable" share link built from a v4 UUID is guessable in the sense that matters: anyone who sees the link has it forever. For access control, use a signed token with an expiry.
Validating one
The version-specific pattern is stricter than the generic one:
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
The generic [0-9a-f]{8}-... pattern accepts any version. If your code requires v4 specifically, check the 4 and the variant character, or you will accept values another system cannot handle.
Questions
How many characters is a UUID?
36 with hyphens, 32 without. The hyphenated form is 8-4-4-4-12.
Can two UUIDs collide?
With a proper random source, effectively never. The realistic failure is a weak random generator, not bad luck.
Should I use UUID v4 or v7?
v7 for database keys because it sorts by time and keeps indexes healthy. v4 when you just need an opaque, coordination-free id.
Is a UUID safe to use as a password reset token?
No. Use a cryptographically random token with an expiry and single use. A UUID is an identifier, not a credential.
Updated 2026-11-16 · written for this site, no trackers, no account.