I created a class that I can use as a random number generator that works better than the standard rand(). Below I've included the .cpp file for my class which contains the class variables std::mt19937 gen and std::uniform_real_distribution distr.
My question is whether it's necessary to create multiple instances of my number generator. For instance if I have classes A and B and each class needs to sample random numbers both within the range [0,1] would it be better if A and B each had its own instance of UniformNumberGenerator or should I take a singleton approach and use one instance for both classes?
UniformNumberGenerator::UniformNumberGenerator(double min, double max)
{
gen = CreateGenerator();
distr = std::uniform_real_distribution<double>(min, max);
}
std::mt19937 UniformNumberGenerator::CreateGenerator()
{
std::random_device rd;
std::mt19937 result(rd());
return result;
}
//Take a sample
double UniformNumberGenerator::operator()()
{
return distr(gen);
}