Header Reference: <random>
Header Reference: <random>
Random-number engines, distributions, seeding facilities, and related helpers from <random>.
Header Reference: <random>
Random-number engines, distributions, seeding facilities, and related helpers from <random>.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
Header reference pages are meant to answer a practical question quickly: what this header provides, when to reach for it, and which usage rules are easiest to get wrong.
<random>std::mt19937, std::mt19937_64, minstd_randuniform_int_distribution, uniform_real_distribution, normal_distribution, bernoulli_distributionstd::random_device and std::seed_seqstd::generate_canonicalUse <random> to generate statistically defined pseudo-random values by combining an engine with a named distribution.
random_device when nondeterminism is desiredrand() for modern C++ code.#include <random>
int main() {
std::mt19937 engine{12345};
std::uniform_int_distribution<int> dist(1, 6);
return dist(engine);
}
The practical rule is to keep the engine as state and apply a distribution that matches the output shape you need.
std::random_device or a seeded seed_seq when you want nondeterministic startup state#include <random>
int main() {
// Start with the primary facility from <random>.
// Then verify lifetime, invalidation, ordering, or error-handling rules.
return 0;
}