Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::uniform_int_distribution<IntType>::operator() not const?

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?

like image 211
Philippe Cayouette Avatar asked Oct 01 '19 21:10

Philippe Cayouette


People also ask

What does uniform_ int_ distribution do in c++?

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.

How does uniform_ int_ distribution work?

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).

How do you create a uniform distribution in C++?

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.


1 Answers

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.

like image 200
NathanOliver Avatar answered Oct 19 '22 08:10

NathanOliver