I am trying to implement the following class:
typedef std::mt19937 Engine;
class Interval
{
public:
double upperBoundary;
double lowerBoundary;
double generateUniformRandomNumber(Engine& engine);
};
I want the class to work in a multithread environment. Each thread will have its own Engine
object instance and it will pass the Engine
to objects of any class that has randomized behaviour.
In order to generate random numbers uniformly the C++11 way, the implementation of the generateUniformRandomNumber
would have to be something like this:
uniform_real_distribution<double> distribution_; // private member of Interval
double Interval::generateUniformRandomNumber(Engine& engine)
{
return distribution_(engine);
}
The problem is that I do not understand C++11 distributions. I know that C++11 random number engines can be very large objects (few kilobytes), but what about distributions? At first I thought that distributions are just simple functors, where operator()
is a pure const
function, but it seems that it is neither pure
nor const
. According to the reference, each distribution instance has a reset()
member function. That means it has a potentionally big internal state or maybe a cache.
My question is:
Do distributions have an internal state? If yes why? Does the standard say anything about the size of this state?
Is it a good idea to make an implementation as I did? Is there a better way?