To properly initialize a pseudo-random number generator to produce the best sequence of pseudo-random numbers, perform the following steps:
- Use an std::random_device to produce random numbers to be used as seeding values:
std::random_device rd;
- Generate random data for all internal bits of the engine:
std::array<int, std::mt19937::state_size> seed_data {};
std::generate(std::begin(seed_data), std::end(seed_data),
std::ref(rd));
- Create an std::seed_seq object from the previously generated pseudo-random data:
std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
- Create an engine object and initialize all the bits representing the internal state of the engine; for example, a mt19937 has 19,937 bits of internal states:
auto eng = std::mt19937{ seq };
- Use the appropriate distribution based on the requirements of the application:
auto dist = std::uniform_real_distribution<>{ 0, 1 };