How to Generate UUIDs in Python and Other Popular Programming Languages

Author:
Zeeshan Ahmed
Publish Date:
October 13, 2025

Introduction

Every modern application whether it's a web app, a mobile game, or a database system - needs a reliable way to create unique identifiers. These identifiers ensures that records, sessions, and objects remain distinct even when distributed across systems or networks.

That's where UUIDs (Universally Unique Identifiers) come in.

In this guide, we'll explain what UUIDs are, how they work, and how to generate UUIDs in Python and other popular programming languages like Javacscript, C#, Java, and C++.

You'll also find examples you can copy and paste directly into your code - and if you need a UUID instantly, you can try DevGizmo's Online UUID Generator.

What Is a UUID?

A UUID (also called a GUID - Globally Unique Identifier in Microsoft terminology) is a 128-bit number used to uniquely identify data. It looks something like this:

550e8400-e29b-41d4-a716-446655440000

UUIDs are represented as 32 hexadecimal digits, displayed in five groups seperated by hyphens.

They are designed to be practically unique across space and time, without requiring a central authority.

There aare several versions of UUIDs, including:

  • Version 1: Based on timestamp and MAC address.
  • Version 3: Name-based (uses MD5 hashing).
  • Version 4: Randomly generated (most common).
  • Version 5: Name-based (uses SHA-1 hashing).

For most application today, UUID v4 is the go-to choice because it's simple, secure, and doesn't reveal system information.

Example With Coding

Generating UUIDs in Python

Python has a built-in library called uuid that makes UUID generation effortless.

Example - Generate a Random UUID (v4)

import uuid

# Generate a random UUID (version 4)
my_uuid = uuid.uuid4()

print(my_uuid)

Output:

e2a1c2f3-06d1-4b1f-ae25-4c2bb3c89ab9

Other Versions:

# Version 1 (time-based)
uuid.uuid1()

# Version 3 (namespace + MD5)
uuid.uuid3(uuid.NAMESPACE_DNS, 'example.com')

# Version 5 (namespace + SHA-1)
uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com')

The uuid module handles all the encoding, randomisation, and formatting for you.

If you're using Python for web APIs, databases, or distributed systems, UUIDs are perfect for creating IDs that won't collide across servers.

Generating UUIDs in JavaScript

In modern JavaScript (ES2021+), generating UUIDs is extremely simple thanks to the built-in crypto module.

Example - Using crypto.randomUUID() (Browser or Node.js 19+)

// Generate a random UUID
const uuid = crypto.randomUUID();
console.log(uuid);

Output:

d7a59d92-2480-4c4c-9a2f-18cb3c9a84b7

For Older Environments

If you're using older Node.js versions or browsers that don't support crypto.randomUUID(), use the popular uuid npm package:

npm install uuid

Then:

const { v4: uuidv4 } = require('uuid');

console.log(uuidv4());

This produces a proper RFC-4122-compliant UUID v4.

Generating UUIDs in C#

C# and .NET come with built-in UUID support via the System namespace, where they are called GUIDs (Globallt Unique Identifiers).

Example - Generate a New GUID

using System;

class Program
{
    static void Main()
    {
        Guid newGuid = Guid.NewGuid();
        Console.WriteLine(newGuid);
    }
}

Output:

c1a7f7c3-cc38-4f8f-9d69-2e50a31b62c3

You can also convert GUIDs to and from strings easily:

string guidString = newGuid.ToString();
Guid parsed = Guid.Parse(guidString);

C# GUIDs are fully compatible with UUIDs defined in RFC 4122, so they can be safely used in databases or APIs that expect UUIDs.

Generating UUIDs in Java

Java includes UUID functionality in the java.util package.

Example - Generate a UUID in Java

import java.util.UUID;

public class UUIDExample {
    public static void main(String[] args) {
        UUID uuid = UUID.randomUUID();
        System.out.println(uuid);
    }
}

Output:

f47ac10b-58cc-4372-a567-0e02b2c3d479

You can also create UUIDs from names or specific byte data using:

UUID.nameUUIDFromBytes("example".getBytes());

Generating UUIDs in C++

C++ doesn't have a native UUID generator in the standard library yet, but you can implement a lightweight random UUID generator manually.

Example - Custom Simple Random UUID (not RFC-compliant)

#include <iostream>
#include <iomanip>
#include <random>
#include <sstream>

std::string generateUUID() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, 15);
    std::uniform_int_distribution<> dis2(8, 11);

    std::stringstream ss;
    ss << std::hex;
    for (int i = 0; i < 8; i++) ss << dis(gen);
    ss << "-";
    for (int i = 0; i < 4; i++) ss << dis(gen);
    ss << "-4"; // version 4
    for (int i = 0; i < 3; i++) ss << dis(gen);
    ss << "-";
    ss << dis2(gen); // variant
    for (int i = 0; i < 3; i++) ss << dis(gen);
    ss << "-";
    for (int i = 0; i < 12; i++) ss << dis(gen);
    return ss.str();
}

int main() {
    std::cout << generateUUID() << std::endl;
}

When and Why to Use UUIDs

UUIDs are ideal when you need:

  • Globally unique IDs (no central server needed).
  • Offline generation of identifiers.
  • Database or API identifiers that won't conflict across systems.
  • Session tokens, tracing IDs, or object keys in distributed environments.

However, be aware that UUIDs are:

  • Longer than integers (128 bits), which can slightly affect database index performance.
  • Random, so not sequential (unless you use UUIDv1 or UUIDv6).

Still, for most modern use cases, UUIDs are the simplest and fastest and safety way to ensure uniqueness.

Try UUIDs Instantly with DevGizmo

If you don't want to open a code editor or library, you can instantly generate secure UUIDs using DevGizmos UUID Generator Tool.

It produces valid version-4 UUIDs and lets you copy them directly for use in your projects - fast, free, and no dependencies required.

Conclusion

UUIDs are one of those tools every developer uses but rarely thinks about - until they need them.

Whether you're coding in Python, JavaScript, C#, C++ or Java, generating a UUID is quick and straightforward. The important part is knowing which version to use andensuring you handle IDs consistently across your systems.

Next time you need a unique identifier, you can either:

Summary

UUIDs (Universally Unique Identifiers) are essential for creating unique IDs in modern software. This post explains what UUIDs are and demonstrates how to generate them in Python, JavaScript, C#, Java, and C++. With code snippets for each language and practical examples, you'll learn how to safely produce random UUIDs for your databases, APIs, and distributed systems. If you just need one instantly, try DevGizmo's free online UUID Generator.

Like this post? Explore more tools built for developers