Let’s generate 10 integers with a uniform distribution between 1 and 6.
Historic approach
#include <cstdlib>
#include <ctime>
#include <iostream>
int main()
{
// Set up generator with seed
srand(time(NULL));
for (int n = 0; n != 10; ++n)
std::cout << rand() % 6 + 1 << ' ';
std::cout << '\n';
}
Only uniform distribution provided. 👎
Modern approach
#include <iostream>
#include <random>
int main()
{
// Set up generator with seed
std::mt19937 gen(std::random_device{}());
// Set up the distribution you want
std::uniform_int_distribution<> dist(1, 6);
for (int n = 0; n != 10; ++n)
std::cout << dist(gen) << ' ';
std::cout << '\n';
}
Multiple distributions provided. 👍
Performance: FlameGraph (Docker)