Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are all random numbers generated between lowest() and max() equal to infinity?

Tags:

c++

random

c++11

Why is u in the program below always infinity?

#include <random>
#include <limits>

int main()
{
    auto seed = std::random_device()();
    std::mt19937 randomEngine(seed);
    const double lo = std::numeric_limits<double>::lowest(); // ~= -1.8e+308
    const double hi = std::numeric_limits<double>::max(); // ~= 1.8e+308
    std::uniform_real_distribution<> U(lo, hi);
    double u = U(randomEngine); // always 1.#INF000000000000
    return 0;
}

It's clearly something to do with the range passed to std::uniform_real_distribution. If I pass it (lo,0) or (0,hi) it generates finite random numbers, but why?

like image 940
TooTone Avatar asked Apr 08 '14 16:04

TooTone


1 Answers

According to N3797:

26.5.8.2.2 [rand.dist.uni.real]

2 Requires: a ≤ b and b − a ≤ numeric_limits<RealType>::max().

This condition is not met since b - a is 2 * numeric_limits<double>::max().

like image 57
SirGuy Avatar answered Nov 15 '22 17:11

SirGuy