The C++ random number generation library, declared in the <random> header, provides random number engines that produce raw pseudo-random bits and distributions that shape those bits into values such as uniform integers, normal doubles or Bernoulli outcomes. It replaces the C functions rand() and srand(), which have poor statistical quality and no control over the output range. You pick an engine, seed it, and pass it to a distribution object.
The library has been part of the standard since C++11 and works the same way in C++20 and C++23. Only the engines and distributions you choose determine the quality, speed and reproducibility of your numbers.
The header separates three roles, and keeping them apart is the main idea of the design.
| Component | Role | Examples |
|---|---|---|
| Seed source | Supplies non-deterministic seed data | std::random_device, std::seed_seq |
| Engine | Produces a stream of pseudo-random unsigned integers | std::mt19937, std::mt19937_64, std::minstd_rand, std::ranlux48 |
| Distribution | Maps engine output to a value range or shape | std::uniform_int_distribution, std::normal_distribution, std::bernoulli_distribution |
An engine on its own gives you numbers in a large fixed range. A distribution takes the engine by reference and converts each call into a value that follows a statistical model. The same distribution works with any engine.
The most common task is a uniform integer in a closed range. Create the engine once, seed it from std::random_device, and reuse both objects.
The output differs on every run because std::random_device supplies a different seed. Both bounds of uniform_int_distribution are inclusive, unlike the half-open range that rand() % n implies. The modulo trick also introduces bias when n does not divide the generator range evenly; the distribution avoids that.
For floating-point values use std::uniform_real_distribution, whose range is half-open: it includes the lower bound and excludes the upper one. The library also ships many other distributions.
Other commonly used distributions are std::binomial_distribution, std::exponential_distribution, std::geometric_distribution, std::discrete_distribution (weighted choice) and std::piecewise_constant_distribution.
For general-purpose code, std::mt19937 is a reasonable default. It has a very long period and passes most statistical tests used in everyday applications. Other engines trade quality for size or speed.
| Engine | Typical use |
|---|---|
std::mt19937 | Default for simulations, games and tests |
std::mt19937_64 | Same algorithm with 64-bit output |
std::minstd_rand | Small state, fast, lower quality |
std::ranlux24, std::ranlux48 | Higher quality, slower |
std::default_random_engine | Implementation-defined, so results differ between compilers |
A bare std::mt19937 gen(rd()) seeds the engine with only 32 bits, although its state holds 19,937 bits. If that matters, fill the state with several values through std::seed_seq:
For reproducible results, such as unit tests or simulations you must replay, seed with a fixed value like std::mt19937 gen(42). The engine sequence is specified by the standard and is the same on every platform. Distribution output is not specified exactly, so the same seed can produce different values with different standard library implementations.
thread_local engines or protect access with a mutex.std::random_device as the main generator. It may be slow, and on some platforms it was historically deterministic. Use it only to seed.std::default_random_engine when you need portable output.The algorithms std::shuffle and std::sample take an engine, which replaces the removed std::random_shuffle.
std::random_shuffle was deprecated in C++14 and removed in C++17 because it relied on rand().
rand() still acceptable in C++?It still compiles, but it has an implementation-defined quality, a global hidden state and no range control. Use <random> for anything beyond a throwaway example.
The engine is seeded with a constant, or std::random_device is deterministic on your platform. Seed from std::random_device for varying output, or print the seed you used when you need to reproduce a run.
std::mt19937 thread-safe?No. Each thread needs its own engine, typically declared thread_local, or access must be synchronised.
No. The engines are predictable once enough output is observed. Use a cryptographically secure source from the operating system or a vetted library.
The <random> library separates engines, which produce raw pseudo-random integers, from distributions, which turn them into useful values. Create one well-seeded engine such as std::mt19937, reuse it, and pair it with std::uniform_int_distribution, std::uniform_real_distribution or another distribution. Use std::shuffle and std::sample for sequences, keep engines per thread, and do not use them for security.