UUID generator · Guide

UUID Generator C++: Options Without a Standard Library

C++ has no UUID in the standard library, so every project picks a library or a platform call. Three options cover nearly all cases, and the fourth — writing your own — is where the bugs are.

Boost.Uuid

The most widely used option, header-only, and covers every UUID version:

CPP
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

boost::uuids::random_generator gen;
boost::uuids::uuid id = gen();
std::string s = boost::uuids::to_string(id);

random_generator uses a cryptographically secure source. Construct it once and reuse it — creating one per call re-seeds and is measurably slower.

stduuid

A lighter header-only library modelled on the proposed standard interface, useful when Boost is too heavy a dependency:

CPP
#include "uuid.h"

std::random_device rd;
auto seed_data = std::array<int, std::mt19937::state_size>{};
std::generate(seed_data.begin(), seed_data.end(), std::ref(rd));
std::seed_seq seq(seed_data.begin(), seed_data.end());
std::mt19937 gen{seq};
uuids::uuid_random_generator uuid_gen{gen};

uuids::uuid id = uuid_gen();
std::string s = uuids::to_string(id);

Platform APIs

With no dependency at all: CoCreateGuid or UuidCreate on Windows, uuid_generate from libuuid on Linux, CFUUIDCreate on macOS. Fine for a single-platform tool, awkward for portable code.

Do not roll your own

std::rand() and an unseeded std::mt19937 are not cryptographically secure and are frequently seeded from the clock — two processes starting in the same second produce identical sequences.

std::random_device is the right source, though on some MinGW builds it is deterministic, which has caused real duplicate-ID bugs. And a hand-written generator usually forgets to set the version and variant bits, producing 128 random bits that are not a valid UUID.

Frequently asked questions

Does C++ have a standard UUID type?

No. Use Boost.Uuid, stduuid, or a platform API.

Is std::mt19937 good enough?

No. It is not cryptographically secure and is often poorly seeded. Use a secure generator.

Why is my hand-written UUID rejected?

Most likely the version and variant bits were not set, so it fails validation.

Ready to try it?

Open the free browser-based UUID generator and apply what you just read — no sign-up, runs locally.

Open the UUID generator tool