HomeArticlesHow ToGenerate a UUID
HOW TO

How to Generate a UUID

Learn how to generate a UUID — understanding v4 random vs v7 time-sortable versions, the standard format, and when to use UUIDs vs auto-increment IDs.

Reviewed by the thecalcu.com team · Last updated August 4, 2026

Free calculators used in this guide

UUID/GUID GeneratorUUID Validator

Overview

A UUID (Universally Unique Identifier) is a 128-bit value used to identify records, sessions, requests, and resources without a central authority handing out sequential numbers. Every modern programming language and database has built-in or library support for generating them, but picking a UUID version, and deciding whether a UUID beats an auto-increment integer at all, carries real performance and design tradeoffs. This guide covers the format, the version choice, generation in code, validation, and how UUIDs stack up against simpler ID schemes.

The UUID Generator handles instant generation in the browser, and the UUID Validator checks the format of any UUID you receive from elsewhere.

What You Need

  • Nothing special for browser-based generation
  • For code-based generation, a JavaScript/Node.js, Python, or other runtime with UUID support (most modern languages have this built in or via a standard library)
  • A rough sense of what the UUID will identify (a database row, a session token, a distributed request ID) so you can pick the right version

Step 1: Understand the UUID Format

A UUID is a 128-bit value, almost always written as 32 hexadecimal digits arranged in five groups separated by hyphens, in an 8-4-4-4-12 pattern:

550e8400-e29b-41d4-a716-446655440000

The full string always runs 36 characters including the four hyphens. The version shows up in the first character of the third group; in the example above, the 4 in 41d4 marks this as a version 4 UUID. A separate variant field (the first bit or two of the fourth group) marks which UUID specification variant is in play, with 8, 9, a, or b as the leading character being the standard RFC variant nearly every modern implementation uses.

Step 2: Choose the Right UUID Version

Several UUID versions exist, but three matter for most work today.

Version 4 (random) comes from 122 bits of randomness, with the remaining 6 bits fixed as version and variant markers. It's the version people reach for most, since it needs no coordination, no machine-specific data, and no timestamp, just a secure random number generator. Use v4 for a simple, general-purpose ID where ordering doesn't matter.

Version 1 (timestamp + MAC address) combines the generating machine's MAC address with a high-precision timestamp. It's fallen out of favor because it leaks machine-identifying information and generation time, a real privacy and security concern once these UUIDs are exposed outside your system.

Version 7 (timestamp-prefixed + random) combines a 48-bit millisecond-precision timestamp with random bits for the rest. That makes v7 UUIDs naturally sortable by creation order, which is why more teams reach for it as a database primary key: it sidesteps the index fragmentation random v4 UUIDs cause in high-write tables.

For most 2026 application development, the practical split is v4 for general-purpose unique IDs where order doesn't matter, and v7 for database primary keys or anywhere insertion order and index efficiency matter.

Step 3: Generate the UUID

The UUID Generator is the fastest option with zero setup. It produces a valid UUID instantly in the browser and supports bulk generation if you need several at once for seeding test data.

To generate UUIDs in code:

JavaScript / Node.js (built-in, no library needed):

const id = crypto.randomUUID();
// e.g. "550e8400-e29b-41d4-a716-446655440000"

crypto.randomUUID() is available natively in Node.js 14.17+ and in every modern browser, and it always generates a version 4 UUID from a cryptographically secure random source.

Python (standard library, no installation needed):

import uuid
id = uuid.uuid4()
# e.g. UUID('550e8400-e29b-41d4-a716-446655440000')

uuid.uuid4() lives in Python's standard uuid module. That same module also has uuid.uuid1() for timestamp/MAC-based UUIDs and uuid.uuid5() for deterministic, name-based UUIDs generated from a namespace and a string via SHA-1 hashing.

For version 7, neither language has shipped a built-in function as of mid-2026 across all runtime versions. Reach for the uuid npm package (uuidv7()) in JavaScript, or the uuid7 or uuid-utils packages in Python.

Step 4: Validate an Existing UUID

Before using a UUID that arrived from somewhere else, an API response, a user-submitted form, a database import, check that it matches the expected format. A valid UUID needs to be exactly 36 characters including hyphens, have those hyphens in the right positions (8-4-4-4-12 grouping), use only hexadecimal characters (0 through 9, a through f, A through F) everywhere else, and carry a valid version digit (commonly 1, 3, 4, 5, or 7) as the first character of the third group.

The UUID Validator checks all of this in one pass and flags exactly which rule failed if the string is malformed, much faster than inspecting it character by character. Validating format before dropping a UUID into a database WHERE clause or using it as a lookup key heads off query errors from malformed input and shrinks the attack surface for poorly sanitised inputs.

Step 5: Decide UUID vs Auto-Increment ID

The choice between a UUID and a plain auto-increment integer touches both your application architecture and your database performance.

UUIDs make sense when multiple servers or services need to generate IDs independently without checking in with a shared counter, which is common in distributed and microservices setups. They also help when you don't want sequential, guessable IDs exposing record count or growth rate in public-facing URLs or APIs, or when records might get created offline and merged later from multiple sources without colliding.

Auto-increment integers make sense when you're running a single database instance with no distributed-write requirement. Storage efficiency matters too here: integers run 4 to 8 bytes against 16 for a UUID, and that adds up fast across millions of rows and foreign-key references. Index performance also favors integers unless you're using UUID v7, since random v4 UUIDs cause measurable fragmentation in high-write, B-tree-indexed tables.

UUID v7 closes much of this gap by pairing the distributed-generation benefit of UUIDs with the sequential-insertion behavior of auto-increment integers, which is why it's becoming the default pick for new database schemas that still want a UUID's collision-avoidance properties.

Common Mistakes to Avoid

Using UUID v1 for externally-facing IDs exposes more than people expect. Version 1 UUIDs leak the generating machine's MAC address and exact creation timestamp, a real privacy and security problem if these UUIDs ever land in a public URL, log file, or API response an attacker can reach.

Using v4 UUIDs as a primary key in high-write databases without weighing the index cost causes trouble at scale. Random v4 UUIDs insert at unpredictable positions throughout a B-tree index, triggering frequent page splits and fragmentation that drag down write performance. If insertion-order locality matters for your workload, switch to UUID v7 or a similar time-ordered identifier like ULID.

Not validating UUID format before using it in queries opens a door you don't need open. Passing an unvalidated, malformed UUID straight into a database query can throw errors, and in poorly sanitised code paths it invites injection-style issues. Validate format first, with the UUID Validator or an equivalent check in code, before using external UUID input in a WHERE clause or as a lookup key.

Formula & Methodology

UUID v4 generation sets two specific groups of bits to fixed version and variant markers, then fills the remaining 122 bits with cryptographically secure random data:

Total bits: 128
Fixed bits: 6 (4 bits for version = 0100 for v4, 2 bits for variant = 10)
Random bits: 122

Collision probability follows the math of the birthday problem. With 122 bits of randomness (2^122 possible values), the number of UUIDs you'd need to generate before hitting a 50% chance of one collision comes out to roughly:

n ≈ 1.42 × √(2^122) ≈ 2.71 × 10^18 (2.71 quintillion)

For scale, even a system generating 1 billion UUIDs per second would take over 85 years to reach that threshold. For any practical software engineering purpose, UUID v4 collision risk is treated as zero. UUID v7 carries the same 122-bit-equivalent randomness in its non-timestamp portion, so its collision resistance holds up just as well, with the bonus of natural time-based ordering for database insertion efficiency.

Frequently Asked Questions

What does a UUID look like?
A UUID is a 128-bit value written as 32 hexadecimal digits, split into five groups separated by hyphens in an 8-4-4-4-12 pattern, for example 550e8400-e29b-41d4-a716-446655440000. The full string, hyphens included, always runs 36 characters. The first character of the third group tells you the UUID version; in this example, the '4' marks it as version 4, or random.
What is the difference between UUID v4 and UUID v7?
UUID v4 comes entirely from random or pseudo-random data, giving 122 bits of randomness with no built-in ordering, so two v4 UUIDs generated a millisecond apart look completely unrelated. UUID v7 prefixes the value with a 48-bit Unix timestamp in milliseconds, then fills the rest with random bits, which makes v7 UUIDs naturally sortable by creation time and far friendlier to database indexes, since new records land near the end of an index instead of scattering at random positions.
Why is UUID v1 considered a privacy risk?
UUID v1 bakes the generating machine's MAC address and the exact generation timestamp straight into the UUID's bits. That means a v1 UUID can reveal which physical device created it and exactly when, a real concern if these UUIDs turn up in URLs, API responses, or logs. That's why v1 has fallen out of favor in most modern applications in favor of v4 (fully random) or v7 (timestamp without machine-identifying data).
How do I generate a UUID in JavaScript or Node.js?
Modern JavaScript and Node.js (14.17 and up) ship a built-in crypto.randomUUID() function that produces a cryptographically secure UUID v4 with no external library needed. Older environments, or anyone needing other versions like v7, can reach for the npm 'uuid' package, which provides uuidv4(), uuidv7(), and other version-specific functions. Browsers support crypto.randomUUID() natively across all modern versions too.
How do I generate a UUID in Python?
Python's built-in uuid module gives you uuid.uuid4() for random UUIDs, the function most people reach for as a general-purpose unique identifier. The same module also has uuid.uuid1() for MAC-address-and-timestamp UUIDs and uuid.uuid5() for name-based UUIDs generated deterministically from a namespace and a string using SHA-1 hashing. As of Python 3.x, uuid ships in the standard library, so nothing extra to install.
What is the probability of two UUIDs colliding?
For UUID v4, which uses 122 random bits, the odds of a collision are close to nothing. Following the math of the birthday problem, you'd need to generate roughly 2.71 quintillion (2.71 × 10^18) UUIDs before even a 50% chance of one duplicate pair shows up. For nearly every practical application, including large distributed systems generating millions of UUIDs a day, collision risk gets treated as effectively zero.
Should I use a UUID or an auto-increment integer as my database primary key?
UUIDs work better in distributed systems, where multiple servers or services need to generate unique IDs on their own without checking in with a central counter, and they don't leak record count or growth rate the way sequential integers do. On the other hand, UUIDs eat more storage (16 bytes versus 4 to 8 for an integer), and random v4 UUIDs in particular fragment B-tree-based database indexes, since new values insert at random positions instead of at the end.
Does UUID v7 solve the database indexing problem of UUID v4?
Mostly, yes. Because UUID v7 leads with a millisecond-precision timestamp, freshly generated v7 UUIDs sort naturally and insert sequentially at the end of a B-tree index, much like auto-increment integers behave. That cuts down the index fragmentation and page-split overhead random v4 UUIDs cause in high-write tables, while still keeping the distributed-generation advantage that made UUIDs appealing in the first place.
What is a nil UUID?
The nil UUID is the special value 00000000-0000-0000-0000-000000000000, every bit set to zero. RFC 9562 (and the earlier RFC 4122) reserve it as a value representing the absence of a valid UUID, similar in spirit to a null pointer or empty string. Applications should treat it as explicitly invalid for representing a real entity, and a UUID validator should flag it separately from a plain malformed UUID.
Are UUIDs case-sensitive?
UUIDs are conventionally written lowercase, but the format itself doesn't care about case. 550E8400-E29B-41D4-A716-446655440000 and 550e8400-e29b-41d4-a716-446655440000 are the exact same value. Most parsing libraries and database systems normalize to lowercase internally for comparison, but string-based comparison or storage without normalization can let mixed-case inconsistency cause two logically identical UUIDs to fail an exact string match.
Can I generate a UUID without writing any code?
The [UUID Generator](/uuid-generator/) produces UUIDs instantly in the browser with nothing to install and no code to write, and it supports v4 (random) along with other common versions, plus bulk generation for cases like seeding test data. It's the fastest route when you just need a one-off UUID for a config file, a test fixture, or quick API testing without opening an editor.
How do I check if a string is a valid UUID?
A valid UUID runs exactly 36 characters including hyphens, follows the 8-4-4-4-12 grouping, uses only hexadecimal characters (0-9, a-f, A-F), and carries a version digit (1, 3, 4, 5, or 7 for the common ones) as the first character of the third group. The [UUID Validator](/uuid-validator/) checks all of this automatically and tells you exactly which rule failed if something's off, which beats eyeballing a 36-character string by hand.

Related Articles

BEST OF

Best UUID / GUID Generators Online 2026

COMPARISON

UUID v4 vs ULID — Which Unique ID Should You Use?

GUIDE

Developer Toolbox Guide — Essential Online Tools

HOW TO

How to Use Cron Expressions

HOW TO

How to Write a robots.txt File