Generate standard UUIDs and GUIDs with customization options for development and testing
Advanced Options for UUID V5
Generated UUIDs
UUID Structure Visualization
xxxxxxxx
time-low
xxxx
time-mid
xxxx
time-high + version
xxxx
clock-seq + variant
xxxxxxxxxxxx
node
Version: – |
Variant: –
Implementation Code Samples
// Using the uuid package
// Install: npm install uuid
// For UUID v4 (random)
import { v4 as uuidv4 } from 'uuid';
const uuid = uuidv4();
console.log(uuid); // e.g., '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
// For UUID v1 (timestamp-based)
import { v1 as uuidv1 } from 'uuid';
const uuidTimestamp = uuidv1();
console.log(uuidTimestamp);
// For UUID v5 (namespace)
import { v5 as uuidv5 } from 'uuid';
// Define namespace (predefined ones from RFC)
const DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
// Generate a UUID for a specific name in the namespace
const uuid = uuidv5('example.com', DNS);
console.log(uuid);
# Using the uuid module (included in Python standard library)
import uuid
# For UUID v4 (random)
random_uuid = uuid.uuid4()
print(random_uuid) # e.g., '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
# For UUID v1 (timestamp-based)
timestamp_uuid = uuid.uuid1()
print(timestamp_uuid)
# For UUID v5 (namespace)
# Use predefined namespaces from RFC
namespace_dns = uuid.NAMESPACE_DNS
# Generate a UUID for a specific name in the namespace
name_uuid = uuid.uuid5(namespace_dns, 'example.com')
print(name_uuid)
# Converting to string with different formats
str_uuid = str(random_uuid) # Regular format with hyphens
hex_uuid = random_uuid.hex # No hyphens
print(f"With hyphens: {str_uuid}")
print(f"Without hyphens: {hex_uuid}")
// Using the java.util.UUID class (included in Java)
import java.util.UUID;
public class UUIDExample {
public static void main(String[] args) {
// For UUID v4 (random)
UUID randomUUID = UUID.randomUUID();
System.out.println("Random UUID: " + randomUUID);
// For UUID v3 (name-based, MD5) or v5 (name-based, SHA-1)
// Java doesn't have built-in namespace UUIDs, but you can create them
// by using an existing UUID as the namespace
// Getting parts of the UUID
System.out.println("Most significant bits: " + randomUUID.getMostSignificantBits());
System.out.println("Least significant bits: " + randomUUID.getLeastSignificantBits());
System.out.println("Version: " + randomUUID.version());
System.out.println("Variant: " + randomUUID.variant());
// Converting to string with different formats
String standardFormat = randomUUID.toString();
String noHyphens = standardFormat.replace("-", "");
System.out.println("With hyphens: " + standardFormat);
System.out.println("Without hyphens: " + noHyphens);
}
}
// Using System.Guid for UUID generation
using System;
class UUIDExample
{
static void Main()
{
// For UUID v4 (random)
Guid randomGuid = Guid.NewGuid();
Console.WriteLine($"Random UUID: {randomGuid}");
// Converting to string with different formats
string standardFormat = randomGuid.ToString();
string noHyphens = randomGuid.ToString("N");
string braces = randomGuid.ToString("B");
string parentheses = randomGuid.ToString("P");
Console.WriteLine($"Standard format: {standardFormat}");
Console.WriteLine($"No hyphens: {noHyphens}");
Console.WriteLine($"With braces: {braces}");
Console.WriteLine($"With parentheses: {parentheses}");
// Converting from string
if (Guid.TryParse("9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d", out Guid parsedGuid))
{
Console.WriteLine($"Parsed UUID: {parsedGuid}");
}
}
}
<?php
// Using PHP's built-in function for UUID v4 (random) in PHP 7.4+
function uuidv4() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,
// 48 bits for "node"
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
// Using ramsey/uuid library (recommended)
// Install: composer require ramsey/uuid
// require 'vendor/autoload.php';
// use Ramsey\Uuid\Uuid;
// // For UUID v4 (random)
// $uuid4 = Uuid::uuid4();
// echo "Random UUID: " . $uuid4->toString() . "\n";
// // For UUID v1 (timestamp-based)
// $uuid1 = Uuid::uuid1();
// echo "Timestamp UUID: " . $uuid1->toString() . "\n";
// // For UUID v5 (namespace)
// $uuid5 = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'example.com');
// echo "Namespace UUID: " . $uuid5->toString() . "\n";
// Generate a UUID
$uuid = uuidv4();
echo "Generated UUID: " . $uuid . "\n";
// Format options
$uuidWithoutHyphens = str_replace('-', '', $uuid);
$uuidUppercase = strtoupper($uuid);
$uuidWithBraces = '{' . $uuid . '}';
echo "Without hyphens: " . $uuidWithoutHyphens . "\n";
echo "Uppercase: " . $uuidUppercase . "\n";
echo "With braces: " . $uuidWithBraces . "\n";
?>
About UUIDs (Universally Unique Identifiers)
What is a UUID?
A UUID (Universally Unique Identifier) is a 128-bit identifier that is guaranteed to be unique across space and time.
UUIDs are standardized by the Open Software Foundation (OSF) as part of the Distributed Computing Environment (DCE).
They are commonly used as database keys, session identifiers, transaction IDs, and in any scenario where a unique identifier is needed
without requiring a central authority to issue them.
UUID Versions
UUID v1 (Time-based): Generated using the current timestamp and the MAC address of the computer.
UUID v4 (Random): Generated using random or pseudo-random numbers. Most commonly used version.
UUID v5 (Name-based, SHA-1): Generated by hashing a namespace identifier and name.
UUID Format
A standard UUID is represented as 32 hexadecimal digits, displayed in five groups separated by hyphens in the form
8-4-4-4-12 for a total of 36 characters (including hyphens). For example:
550e8400-e29b-41d4-a716-446655440000
Common Use Cases
• Database primary keys
• Distributed systems for generating unique identifiers
• Session identifiers in web applications
• Transaction IDs in financial systems
• Object identifiers in content management systems