Guide · UUID Generator

What is a UUID? Format, versions and collision odds explained

What is a UUID: the 128-bit format, how to read the version and variant digits, v4 vs v7 for database keys, real collision odds and code for JS, Python and SQL.

By Kola Oguntade · Updated November 2026 · 6 min read
550e8400-e29b-41d4v4

A UUID (universally unique identifier) is a 128-bit number used to label something, such as a database row or a file, so that no two labels clash, without any central authority handing them out. It’s written as 32 hexadecimal digits in five groups separated by hyphens, like f81d4fae-7dec-11d0-a765-00a0c91e6bf6. In short: a very large number anyone can generate and expect to be unique.

The current standard is RFC 9562, published in May 2024. It replaced RFC 4122, kept the old versions and added three new ones (6, 7 and 8). It also confirms that GUID, the name used across Microsoft products, refers to the same thing.

How a UUID is laid out

The text form is always 36 characters: 32 hex digits plus 4 hyphens, grouped 8-4-4-4-12. Each hex digit carries 4 bits, so 32 digits make 128 bits, or 16 bytes. Upper and lower case are both valid.

Most of those bits change depending on the version, but two small fields sit in the same place in every standard UUID:

  • The version is the 13th hex digit, the first one in the third group. It tells you how the UUID was made.
  • The variant is the 17th hex digit, the first one in the fourth group. For UUIDs that follow RFC 9562 it’s always 8, 9, a or b, because the top two bits are fixed at binary 10.

Take the example from the PostgreSQL documentation, 019535d9-3df7-79fb-b466-fa907fa17f9e. The third group starts with 7, so it’s a version 7 UUID. The fourth group starts with b, which is the standard variant. And because version 7 puts a Unix timestamp in the first 48 bits, you can decode the first 12 hex digits (019535d93df7) as 1,740,365,184,503 milliseconds, which is 02:46:24.503 UTC on 24 February 2025. The example at the top of this page has a 1 in the version slot and an a in the variant slot, so it’s a version 1 UUID.

Because the version and variant use 6 bits, a random UUID has 122 bits left for randomness.

The UUID versions and what each one is for

RFC 9562 defines eight versions. You’ll meet v4 and v7 most often, v1 in older systems, and the others only now and then.

VersionHow it’s madeTypical use
160-bit timestamp counted in 100-nanosecond steps from 1582, plus a clock sequence and a 48-bit node ID (often the MAC address)Legacy systems
2Reserved for DCE Security; not detailed in the RFCRare
3MD5 hash of a namespace and a nameThe same input always gives the same UUID
4122 random bitsGeneral-purpose IDs
5SHA-1 hash of a namespace and a nameLike v3, preferred over it
6Version 1’s fields reordered so they sort by timeSystems already using v1
748-bit Unix timestamp in milliseconds, then random bitsDatabase keys and anything that should sort by creation time
8Custom layout; only version and variant are fixedVendor or experimental formats

There are also two special values: the Nil UUID, which is all zeros, and the Max UUID, which is all ones (every hex digit is f). They’re handy as ‘no value’ or ‘upper bound’ markers.

The RFC is blunt about version 1. MAC addresses raise privacy problems, and the standard says implementations should use version 7 instead of 1 or 6 where they can.

How likely is a duplicate UUID?

A version 4 UUID has 122 random bits, which gives 2¹²² possible values, about 5.3 × 10³⁶. Duplicates follow the birthday problem: the chance of a clash grows with the square of how many you’ve made, so it rises faster than you’d expect, but from a tiny base.

Using the standard approximation, p ≈ 1 − exp(−n²/2N) with N = 2¹²², you’d need about 2.71 × 10¹⁸ UUIDs (2.7 quintillion) for a 50% chance of even one duplicate. Generate a billion a second and that takes about 86 years.

UUIDs generatedChance of at least one duplicate
1 millionabout 1 in 10²⁵ (9.4 × 10⁻²⁶)
1 billion9.4 × 10⁻²⁰
1 trillion9.4 × 10⁻¹⁴
1 quadrillion9.4 × 10⁻⁸
2.71 × 10¹⁸50%

The maths assumes a good random source. Real duplicates come from weak or badly seeded generators, or from someone pasting the same ID into two places. Random collisions aren’t worth worrying about; bugs are, which is why a unique constraint on the column is still a good idea.

Unique is not the same as secret. RFC 9562 says UUIDs shouldn’t be assumed hard to guess and must not be used as security capabilities. For a password reset link or an API key, use a dedicated random token; the password generator is a better starting point for anything that has to stay private.

UUID v4 or v7 for database keys

This is the decision that matters in practice. Both are 128 bits and both are safe from collisions in practice. The difference is order.

Random v4 keys land at random places in a B-tree index, so every insert can touch a different page. RFC 9562 says the performance cost of that on B-trees ‘can be dramatic’, and that time-ordered values can make a difference of an order of magnitude or more. Version 7 starts with the timestamp, so keys created one after another sit next to each other and sort correctly as plain bytes or as text.

My view: use v7 for primary keys in new tables. Pick v4 when the ID will be public and you don’t want it to reveal when the record was created, because anyone can read the timestamp out of a v7 UUID, as the example above showed. Whichever you choose, store it in a native UUID or 16-byte binary column rather than as text. The RFC points out that the text form takes 288 bits to hold a 128-bit value.

How to generate a UUID in JavaScript, Python and PostgreSQL

JavaScript

In the browser, crypto.randomUUID() returns a 36-character version 4 string from a cryptographically secure generator. MDN notes it only works in secure contexts (HTTPS or localhost). Node.js has the same function in its crypto module from versions 14.17.0 and 15.6.0, and the Node.js docs list crypto.randomUUIDv7() from version 26.1.0.

const id = crypto.randomUUID();

Python

The standard library’s uuid module has uuid.uuid4() for random UUIDs. Python 3.14 added uuid.uuid7(), which embeds a 48-bit timestamp and uses a counter to keep IDs in order within the same millisecond. You can check any UUID’s version with the .version attribute.

import uuid; uuid.uuid4(); uuid.uuid7()

PostgreSQL

gen_random_uuid() has been built in since PostgreSQL 13 and returns a version 4 UUID. PostgreSQL 18 added uuidv4(), an alias for the same thing, and uuidv7() for time-ordered keys.

CREATE TABLE orders (id uuid PRIMARY KEY DEFAULT uuidv7());

Make UUIDs without writing code

If you only need a few IDs for test data, a config file or a quick fix, the UUID generator makes up to 1,000 at once, as random v4 or time-ordered v7, with or without hyphens and in upper or lower case. It uses your browser’s secure random generator, nothing is uploaded, and it’s free with no sign-up. Before you paste a batch into a seed script, check the 13th character: if you asked for v7 and see a 4, you’ve generated version 4 UUIDs by mistake.

Working with IDs inside JSON? The JSON formatter makes long payloads full of UUIDs much easier to scan.

Also useful: Hash Generator · Base64 Encode and Decode · URL Encoder and Decoder

Questions people ask

Is a GUID the same as a UUID?

Yes. GUID (globally unique identifier) is the name Microsoft uses, and RFC 9562 treats the two terms as the same thing: a 128-bit value written as 32 hexadecimal digits. One difference to know is that Microsoft COM stores some fields in little-endian byte order when saving GUIDs as binary.

Can two UUIDs be the same?

In theory, yes. A version 4 UUID has 122 random bits, so you would need about 2.7 quintillion of them for a 50% chance of a single duplicate. In practice, duplicates come from broken random number generators or copied code, not from bad luck.

How can I tell which version a UUID is?

Look at the first digit of the third group, the 13th hex digit overall. A 4 means random, a 7 means time-ordered, a 1 means the older time-and-MAC-address format. The first digit of the fourth group should be 8, 9, a or b for a standard UUID.

Should I use UUID v4 or v7 for a primary key?

For new database tables, v7 is usually the better choice. It starts with a millisecond timestamp, so new keys sort together and index inserts stay efficient. Use v4 when you don’t want the ID to reveal when a record was created.

Are UUIDs secure enough to use as secret tokens?

No. RFC 9562 says implementations should not assume UUIDs are hard to guess and must not use them as security capabilities, meaning identifiers whose mere possession grants access. For password reset links or API keys, use a purpose-built random token instead.

More guides