As I understand it, a distribution should not change when used to pull a random number. For a uniform distribution for example, its min/max should not change as we use it to generate random numbers, so why operator() is not const?
C++ have introduced uniform_int_distribution class in the random library whose member function give random integer numbers or discrete values from a given input range with uniform probability.
std::uniform_int_distribution This distribution produces random integers in a range [a,b] where each possible value has an equal likelihood of being produced. This is the distribution function that appears on many trivial random processes (like the result of rolling a die).
C++11 and generation over a range const int range_from = 0; const int range_to = 10; std::random_device rand_dev; std::mt19937 generator(rand_dev()); std::uniform_int_distribution<int> distr(range_from, range_to); std::cout << distr(generator) << '\n'; And here's the running example.
While min()
and max()
wont change, the distribution may contain state that helps it generate the next value. If operator()
were const
then this state could not be modified without having to guarantee that the object is thread safe. Providing that guarantee could be expensive and distributions are meant to be light weight.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With