What is UUID v4?
UUID v4 is the most widely used UUID version. It consists of 122 bits of cryptographically secure randomness plus 6 fixed bits that encode the version (4) and variant (RFC 4122). The result is a 128-bit identifier with an astronomically small probability of collision.
In browsers, v4 is generated using crypto.randomUUID() — a native Web Crypto API call that draws from the operating system's CSPRNG, the same entropy source used for TLS keys. No external dependencies are needed.
A v4 UUID looks like: 550e8400-e29b-41d4-a716-446655440000
Pros and Cons of UUID v4
✓ Strengths
- Globally unique (overwhelming probability)
- No privacy leak — no timestamp, no MAC address
- Supported natively in all modern browsers and runtimes
- Simple: no configuration needed
✗ Limitations
- Not sortable by creation time
- Causes B-tree index fragmentation in databases
- Consider UUID v7 for new database primary keys
UUID v4 Code Examples
How to generate a UUID v4 in your preferred language:
// Browser — native, zero dependencies
const id = crypto.randomUUID();
console.log(id);
// → "550e8400-e29b-41d4-a716-446655440000"
// Node.js 14.17+
import { randomUUID } from 'crypto';
const id = randomUUID();
// npm install uuid
import { v4 as uuidv4 } from 'uuid';
const id = uuidv4();
import uuid # Built-in, no install needed
unique_id = str(uuid.uuid4())
print(unique_id)
# → "550e8400-e29b-41d4-a716-446655440000"
# Without hyphens
compact = uuid.uuid4().hex
# Django model field:
# id = models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True)
// go get github.com/google/uuid
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
id := uuid.New() // UUID v4
fmt.Println(id.String())
// → "550e8400-e29b-41d4-a716-446655440000"
}
import java.util.UUID; // Built-in since Java 1.5
UUID id = UUID.randomUUID();
System.out.println(id.toString());
// → "550e8400-e29b-41d4-a716-446655440000"
// Spring Boot / JPA:
// @Id
// @GeneratedValue(strategy = GenerationType.UUID)
// private UUID id;
<?php
// composer require ramsey/uuid
use Ramsey\Uuid\Uuid;
$id = Uuid::uuid4()->toString();
echo $id;
// → "550e8400-e29b-41d4-a716-446655440000"
// Laravel helper:
// $id = (string) Str::uuid(); // v4
Frequently Asked Questions
How unique is UUID v4? Can two systems generate the same one?
In practice, UUID v4s are universally unique. With 122 bits of randomness there are roughly 5.3 × 10³⁶ possible values. To have a 50% chance of a single collision you'd need to generate about 2.7 × 10¹⁸ UUIDs — at one billion per second that would take over 85 years. For all real-world purposes treat v4 UUIDs as globally unique.
Is it safe to generate UUID v4 in the browser?
Yes. This tool uses crypto.randomUUID(), part of the Web Crypto API built into all modern browsers. It draws randomness from the OS's CSPRNG — the same source used for TLS keys. Everything happens locally; nothing is sent to our servers.
Should I use UUID v4 or v7 for database primary keys?
For new projects, prefer UUID v7. It encodes a Unix millisecond timestamp in the first 48 bits, making rows insert in sequential order in B-tree indexes — no fragmentation. UUID v4 inserts at random positions, causing costly page splits. For existing projects already using v4, there's no urgent reason to switch unless index performance is a concern.
What's the difference between UUID and GUID?
Nothing in practice. GUID (Globally Unique Identifier) is Microsoft's term for the same concept. Both follow RFC 4122, produce the same 128-bit format, and are fully interchangeable.
Recently Generated
Your last 10 generations are saved locally in your browser. Nothing is sent to our servers.