I am trying to generate randoms within a range. I have multiple objects in multiple threads that would be calling this function, the function is in a singleton class so there is only one object that is called. But I noticed that my random numbers are closer to the middle of the range and by consequence, to each other for the first 200 calls, then they start to slowly disperse over the given range.
- Is there a harm in re-instantiating and re-seeding every time the function called? i did read this answer but didn't fully understand it.
- I tried to keep
std::uniform_real_distribution<double> dis
as a class variable but i kept getting errors thatuniform_real_distribution
is not a member ofstd::
why is that ? - I tried to keep
std::shared_ptr< std::uniform_real_distribution<double> > _uniform_distribution
as a class variable, and still got errors. - What is the correct way to reference a random generator?
- What is the right solution for this problem?
double getRandom(Object* foo){
std::random_device rnd;
std::mt19937 gen(rnd());
std::uniform_real_distribution<double> dis(0, foo->limit);
double random = dis(gen);
return random;
}