Coding

Random GUID Generator: What a UUID/GUID Is and How to Make One

A random GUID generator produces a 128-bit identifier written as 32 hexadecimal digits, normally a version 4 UUID such as 0f8fad5b-d9cb-469f-a165-70867728950e. To make one right now, run New-Guid in PowerShell, crypto.randomUUID() in a browser console, or python -c "import uuid; print(uuid.uuid4())".

If you only need a value to paste somewhere, the uuid generator on larely.com makes them in your browser. The rest of this article covers what the format means, the one-line commands for common languages, and when a random GUID is the wrong tool.

GUID or UUID: are they different?

No. RFC 9562, the current UUID standard, describes UUIDs (Universally Unique IDentifiers) as "also known as GUIDs (Globally Unique IDentifiers)". "GUID" is the name Microsoft uses in Windows, .NET, SQL Server and PowerShell. "UUID" is the name used in the standard and in most other ecosystems. A value generated by one is valid in the other.

The one place they can diverge is binary storage. The RFC notes that Microsoft's COM GUIDs use little-endian byte order when saved, while the standard's byte layout is big-endian. If you move raw 16-byte values between a Windows system and another system, check byte order. The text form is the same everywhere.

What the format looks like

The text form is five groups of hexadecimal digits separated by hyphens, in an 8-4-4-4-12 pattern, 36 characters in total. Case does not matter: the RFC says the letters may be upper, lower or mixed case.

0f8fad5b-d9cb-469f-a165-70867728950e
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

Two positions in a random GUID are not random. M is the version: the RFC places it in the most significant 4 bits of octet 6, which is the first digit of the third group. In the example, that digit is 4, so it is a version 4 UUID. N holds the variant: bits 64 and 65 are set to 1 and 0, so the first digit of the fourth group is always 8, 9, a or b. In the example it is a.

That leaves 122 random bits out of 128. The number of possible values is 2122, roughly 5.3 x 1036. This is why random GUIDs can be generated independently on many machines without a central counter and still be treated as unique.

Generate a random GUID in PowerShell

New-Guid

Microsoft's documentation says the cmdlet "creates a random globally unique identifier (GUID)", and that without string input or -Empty it creates a Version 4 UUID. It returns a GUID object. To get a plain string, for example to paste into a config file:

(New-Guid).ToString()

Example output:

d61bbeca-0186-48fa-90e1-ff7aa5d33e2d

New-Guid -Empty returns the all-zero GUID. New-Guid -InputObject "<string>" does not generate anything: it converts a string that already holds a GUID into a GUID object.

Generate a random GUID in C# and .NET

Guid id = Guid.NewGuid();
Console.WriteLine(id);

Per the .NET documentation, Guid.NewGuid() creates a Version 4 UUID as described in RFC 4122, and the result is guaranteed not to equal Guid.Empty. On Windows it wraps the CoCreateGuid function. On non-Windows platforms, starting with .NET 6, it calls the operating system's cryptographically secure random number generator. In earlier .NET versions, the documentation says the entropy is not guaranteed to come from a CSPRNG.

Generate a random UUID in JavaScript

crypto.randomUUID()

Paste that into the browser developer console. MDN describes it as generating a v4 UUID with a cryptographically secure random number generator, returned as a 36-character string. It is available only in secure contexts, meaning pages served over HTTPS, and also in Web Workers. MDN lists it as widely available across browsers since March 2022. Example output:

36b8f84d-df4e-4d49-b662-bcde71a8764f

On a page served over plain HTTP, crypto.randomUUID is not available, so use another source or serve the page over HTTPS.

Generate a random UUID in Python

import uuid

print(uuid.uuid4())

The Python documentation says uuid.uuid4() generates "a random UUID in a cryptographically-secure method according to RFC 9562". str(uuid.uuid4()) gives the hyphenated 36-character form. Be careful with uuid.uuid1(): the same page warns that it may compromise privacy because it creates a UUID containing the computer's network address.

Which version should you pick?

RFC 9562 defines several versions. Three matter for most developers:

VersionWhat it containsUse it when
v1A 60-bit timestamp (100-nanosecond intervals, UTC) plus node dataYou are maintaining a system that already uses it
v4122 random bitsYou want an opaque unique ID and ordering does not matter
v7A 48-bit Unix timestamp in milliseconds in the top bits, then random dataYou want IDs that sort by creation time, for example as database keys

For a "random GUID generator", v4 is what you get by default and what almost every tool means. Version 7 is newer. Python added uuid.uuid7() in version 3.14, and it is described as embedding a 48-bit timestamp with a counter to keep values monotonic within a millisecond. Check that your language or library supports v7 before relying on it. Support is not as universal as v4.

The trade-off with v7 is that the value reveals roughly when it was created, because the timestamp is stored in the clear. A v4 value carries no such information.

When not to use a random GUID

A GUID is an identifier, not a secret. The security section of RFC 9562 is direct about this: implementations "SHOULD NOT assume that UUIDs are hard to guess", and UUIDs "MUST NOT be used as security capabilities", meaning identifiers whose mere possession grants access. A v4 UUID is unpredictable if the generator is a CSPRNG, but that is not the same as being designed as a credential.

Microsoft gives two further reasons in the Guid.NewGuid documentation. A version 4 UUID has a partially predictable bit pattern, so it cannot serve as a proper cryptographic pseudo-random function. And it uses at most 122 bits of entropy, while some cryptographic components require 128 bits or more. For keys, tokens and passwords, use the language's cryptographic random generator instead; in .NET, Microsoft points to the RandomNumberGenerator class. For passwords specifically, the password generator on larely.com is the better fit.

Two further cases call for something else. If you need a short, human-readable ID (an order number, a ticket reference), a 36-character GUID is a poor choice. And if you need uniqueness guaranteed by construction, such as a strictly increasing sequence, a database sequence does that; a random GUID only makes a collision astronomically unlikely.

Common formatting questions

  • Braces and no hyphens. Windows tools sometimes show GUIDs in braces, {0f8fad5b-d9cb-469f-a165-70867728950e}, and some systems store them without hyphens as 32 digits. The PowerShell docs show that New-Guid -InputObject accepts the hyphenated form and the 32-digit form. These are the same value written differently.
  • Upper or lower case. The RFC treats them as equal, but plain string comparison does not. Normalise to one case before comparing GUIDs as text.
  • Validating input. A string matching the 8-4-4-4-12 pattern is well-formed, but that says nothing about whether it was generated randomly. Check the version digit if you need v4 specifically.

Frequently asked questions

Is a GUID the same as a UUID?
For practical purposes, yes. RFC 9562 defines UUIDs and says they are also known as GUIDs. The name GUID is mostly used in Microsoft products. The one difference to watch for is byte order: Microsoft's COM GUIDs are stored little-endian in binary form, so the raw bytes can differ from a big-endian UUID even though the text form is the same.
Can two random GUIDs be the same?
In theory yes, in practice no. A version 4 UUID has 122 random bits, which gives about 5.3 x 10^36 possible values. A collision needs a faulty random source or a bug, not bad luck, as long as the generator uses a cryptographically secure random number generator.
Is it safe to use a GUID as a password, API key or access token?
No. RFC 9562 says implementations should not assume UUIDs are hard to guess and that UUIDs must not be used as security capabilities, meaning identifiers whose mere possession grants access. Microsoft also advises against using Guid.NewGuid for cryptographic purposes. Use a dedicated cryptographic random generator for secrets.
What is the difference between UUID v4 and v7?
Version 4 is almost entirely random. Version 7 puts a 48-bit Unix timestamp in milliseconds in the most significant bits and fills the rest with random data, so values sort roughly by creation time. Python added uuid.uuid7() in version 3.14.
What does the all-zero GUID mean?
00000000-0000-0000-0000-000000000000 is the nil UUID, with all 128 bits set to zero. It is used as an 'empty' or 'no value' marker. PowerShell can produce it with New-Guid -Empty, and .NET exposes it as Guid.Empty.

Sources