UUID v4 Generator: How Random UUIDs Work and When to Use Them
A UUID v4 is a 128-bit identifier built almost entirely from random bits. It is the most common UUID version because it needs no coordination: any server, browser or phone can create one and be confident it will never collide with an ID created anywhere else. This generator uses your browser's cryptographically secure random source, so nothing is sent to a server.
What a v4 UUID looks like
Every UUID is written as 32 hexadecimal characters split into five groups (8-4-4-4-12). In a version 4 UUID the first character of the third group is always 4, and the first character of the fourth group is 8, 9, a or b. Those fixed bits mark the version and variant; the remaining 122 bits are random.
f47ac10b-58cc-4372-a567-0e02b2c3d479
^ ^
version variantHow likely is a collision?
With 122 random bits there are about 5.3 × 10^36 possible values. You would need to generate roughly 2.7 quintillion UUIDs before the chance of a single duplicate reached 50%. For practical purposes, as long as the random source is secure, collisions do not happen.
The risk only becomes real when a weak generator is used, for example Math.random() in old JavaScript code. Prefer crypto.randomUUID() in browsers and Node.js 19+, uuid.uuid4() in Python, or UUID.randomUUID() in Java.
UUID v4 vs v1 vs v7
v1 embeds a timestamp and the machine's MAC address, which can leak information. v4 is fully random and private. v7 (standardised in RFC 9562) starts with a Unix millisecond timestamp followed by random bits, so values sort in creation order.
Use v4 for public tokens, file names and API resource IDs. Consider v7 for database primary keys in large tables, where random v4 values fragment B-tree indexes and slow down inserts.
Generating UUID v4 in code
If you need UUIDs inside your application rather than copying them from this page, every major language has a built-in option:
// JavaScript (browser & Node 19+)
crypto.randomUUID();
# Python
import uuid; uuid.uuid4()
-- PostgreSQL 13+
SELECT gen_random_uuid();Common mistakes
Storing UUIDs as 36-character text wastes space; PostgreSQL has a native uuid type and MySQL can store them as BINARY(16). Do not use UUIDs as secrets on their own: they are unique, but not designed to be unguessable authentication tokens. And never truncate a UUID to make it shorter, because that destroys its uniqueness guarantee.