liveBook · Manning

Std::random_device

As explained in Learn C++ by Example

std::random_device is a C++ standard library generator interface that supplies nondeterministic random values when supported by the implementation.

std::random_device is a C++ standard-library generator that supplies nondeterministic random values when the implementation supports them. Its practical role is usually seeding a separate random-number engine rather than producing every value directly. section 3.2.1

Seed an engine

std::random_device rd;\nstd::mt19937 engine(rd());\n\nThe device supplies an initial seed; the engine then produces the sequence used with a distribution such as std::uniform_int_distribution<int>. section 3.2.1

Use the interface with these practical cautions:

  • Check several returned values: some older implementations always returned 0, and an implementation may repeat a sequence. section 3.2.1
  • Prefer it as a seed source when repeated calls could begin producing the same values. section 3.2.1

How Learn C++ by Example covers it

How deep it goes
Defines nondeterministic behavior and implementation caveats, but stops short of detailed entropy-source internals.
Examples
Uses std::random_device rd; with std::mt19937 engine(rd()) and a uniform integer distribution.
What it assumes
Assumes random-number engines and distributions, while introducing their required separation and seeding relationship.
What you can do after
Apply it via the C++ random library to seed an engine and investigate implementation-specific output.
How it connects
Connects random-number engines, seeds, distributions, and std::mt19937.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.