Using the generator for multiple distributions

suggest change

The random number generator can (and should) be used for multiple distributions.

#include <iostream>
#include <random>

int main()
   std::default_random_engine pseudo_random_generator;
   std::uniform_int_distribution<int> int_distribution(0, 9);
   std::uniform_real_distribution<float> float_distribution(0.0, 1.0);
   std::discrete_distribution<int> rigged_dice({1,1,1,1,1,100});
   std::cout << int_distribution(pseudo_random_generator) << std::endl;
   std::cout << float_distribution(pseudo_random_generator) << std::endl;
   std::cout << (rigged_dice(pseudo_random_generator) + 1) << std::endl;
   return 0;

In this example, only one generator is defined. It is subsequently used to generate a random value in three different distributions. The rigged_dice distribution will generate a value between 0 and 5, but almost always generates a 5, because the chance to generate a 5 is 100 / 105.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents