c++ - How many random numbers does std::uniform_real_distribution use? -


i surprised see output of program:

#include <iostream> #include <random>  int main() {     std::mt19937 rng1;     std::mt19937 rng2;     std::uniform_real_distribution<double> dist;      double random = dist(rng1);     rng2.discard(2);      std::cout << (rng1() - rng2()) << "\n";      return 0; } 

is 0 - i.e. std::uniform_real_distribution uses two random numbers produce random double value in range [0,1). thought generate 1 , rescale that. after thinking guess because std::mt19937 produces 32-bit ints , double twice size , not "random enough".

question: how find out number generically, i.e. if random number generator , floating point type arbitrary types?

edit: noticed use std::generate_canonical instead, interested in random numbers of [0,1). not sure if makes difference.

for template<class realtype, size_t bits, class urng> std::generate_canonical standard (section 27.5.7.2) explicitly defines number of calls uniform random number generator (urng) be

max(1, b / log_2 r),

where b minimum of number of bits in mantissa of realtype , number of bits given generate_canonical template parameter. r range of numbers urng can return (urng::max()-urng::min()+1). however, in example not make difference, since need 2 calls mt19937 fill 53 bits of mantissa of double.

for other distributions standard not provide generic way information on how many numbers urng has generate obtain 1 number of distribution.

a reason might distributions number uniform random numbers required generate single number of distribution not fixed , may vary call call. example std::poisson_distribution, implemented loop draws uniform random number in each iteration until product of these numbers has reached threshold (see example implementation of gnu c++ library (line 1523-1528)).


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -