How can I get a uniformly distributed random number from the interval [0, 1] in C++? -


this question has answer here:

how can uniformly distributed random number interval [0, 1]?

i using following code, in c , know how 1 things in c++ instead.

double rnd() {     return ((double)rand()/rand_max); } 

this tricky correctly. integers, uniform distribution class produces numbers in range [min...max], real numbers, range [min ... max). [0 .. 1.0], need add small number upper end of range. happens, since our number 1.0, standard library gives correct number add epsilon() target type:

std::mt19937_64 gen{ std::random_device()() }; std::uniform_real_distribution<double> dis{     0.0,     1.0 + std::numeric_limits<double>::epsilon() }; 

epsilon smallest number can added 1.0 , produce result compares greater 1.0, allows numbers (and including) 1.0, no greater--precisely requested range.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -