Skip straight to the UUID v4 generator or keep reading to understand random UUID generation.
UUID v4 is the most common UUID version for random identifiers. It is generated from random or pseudo-random numbers and does not include timestamps or machine-specific data. That makes it a good choice when you want strong uniqueness without revealing creation time or generation context.
What is UUID v4?
UUID Version 4 creates a 128-bit identifier using random bits. The UUID generator sets the version and variant bits according to RFC 4122, and fills the rest of the UUID with random data. This gives a very large namespace (about 2122 random values) and makes collisions extremely unlikely.
f47ac10b-58cc-4372-a567-0e02b2c3d479
└───────┘ └──┘ └───┘ └───────────────┘
random 4 variant random
bits bits
Why choose UUID v4?
- Simple and widely supported in most languages and frameworks.
- No embedded time or machine information, which improves privacy.
- Great for distributed systems where random identifiers are enough.
- Excellent for primary keys, tokens, session IDs, and unique file names.
Code examples for UUID v4
Web Crypto API
Modern browsers and Node.js 14.17+ support `crypto.randomUUID()`.
// Browser or Node.js 14.17+
const id = crypto.randomUUID();
console.log(id);
uuid npm package
Works in Node.js and browser environments.
# Install
npm install uuid
import { v4 as uuidv4 } from 'uuid';
const id = uuidv4();
console.log(id);
Python standard library
The built-in `uuid` module can generate UUID v4 values.
import uuid
print(uuid.uuid4())
google/uuid
Generate random UUID v4 values in Go.
# Install
go get github.com/google/uuid
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
id := uuid.New()
fmt.Println(id)
}
Java standard library
Use `java.util.UUID` for random UUID generation.
import java.util.UUID;
public class Main {
public static void main(String[] args) {
UUID id = UUID.randomUUID();
System.out.println(id);
}
}
System.Guid
Generate a random GUID in .NET.
// .NET
var id = Guid.NewGuid();
Console.WriteLine(id);
PHP random_bytes
Generate a UUID v4 in PHP without external packages.
function uuid4(): string {
$data = random_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
echo uuid4();
SecureRandom
Generate a UUID v4 directly in Ruby.
require 'securerandom'
puts SecureRandom.uuid
uuid crate
Generate random UUID v4 values in Rust.
# Cargo.toml
[dependencies]
uuid = { version = "1", features = ["v4"] }
use uuid::Uuid;
fn main() {
let id = Uuid::new_v4();
println!("{}", id);
}
Elixir UUID package
Generate UUID v4 values in Elixir using a UUID library from Hex.
# mix.exs dependencies
{:elixir_uuid, "~> 1.2"}
UUID = Elixir.UUID
id = UUID.uuid4()
IO.puts(id)
Erlang uuid library
Generate UUID v4 values in Erlang with a UUID dependency.
% rebar.config
{deps, [{uuid, "*"}]}
UUID = uuid:uuid4().
io:format("~s~n", [UUID]).
PostgreSQL
Use built-in UUID functions for random UUID generation.
-- PostgreSQL
SELECT gen_random_uuid();
MySQL
MySQL / MariaDB also provide a random UUID function.
-- MySQL
SELECT UUID();
SQL Server
Use `NEWID()` for random GUIDs in SQL Server.
-- SQL Server
SELECT NEWID();
Frequently asked questions
Is UUID v4 truly random?
UUID v4 uses random or pseudo-random bits for most of its value. The version and variant bits are fixed by the UUID standard, but the remaining bits are randomly generated.
Can UUID v4 be used as a database primary key?
Yes, UUID v4 is commonly used for database keys. It is best when ordering doesn’t matter or when you want a fully random identifier with strong uniqueness.
Should I use UUID v4 or UUID v7?
Use UUID v4 for random identifiers with no embedded timestamp. Use UUID v7 when you want time-ordered UUIDs that sort by creation time.
Ready to generate? Open the UUID v4 generator →