To generate pseudo-random numbers in your application, you should perform the following steps:
- Include the header <random>:
#include <random>
- Use anĀ std::random_device generator for seeding a pseudo-random engine:
std::random_device rd{};
- Use one of the available engines for generating numbers and initialize it with a random seed:
auto mtgen = std::mt19937{ rd() };
- Use one of the available distributions for converting the output of the engine to one of the desired statistical distributions:
auto ud = std::uniform_int_distribution<>{ 1, 6 };
- Generate the pseudo-random numbers:
for(auto i = 0; i < 20; ++i)
auto number = ud(mtgen);