1

So I am trying to get uniform random doubles using the following for my random number generation:

std::random_device rd;
std::mt19937_64 randEng(rd());
std::uniform_real_distribution<double> rg(std::numeric_limits<double>::min(), std::numeric_limits<double>::max());

and then of course calling rg(randEng)

However if I use the numeric limits of the double like this I am not getting a random sample that are from -DOUBLE to +DOUBLE. Instead I am getting numbers that are in the order of maginute e+306 or e+307 or e+308. Is there something I am doing wrong here that will not allow me to get the range that I am looking for?

If I put in something else like -10, 10 to the uniform distribution I do get valid numbers in that range. Is there something going on here for doubles that I am missing?

csteifel
  • 2,854
  • 6
  • 35
  • 61

2 Answers2

1
std::numeric_limits<double>::min()

is the double value with smallest absolute value. if you want the smallest negative (highest abs), you can use

-std::numeric_limits<double>::max()

Technically, there may also be

numeric_limits<double>::has_infinity
-numeric_limits<double>::infinity()

But that does not seem to be what you want

b.buchhold
  • 3,837
  • 2
  • 24
  • 33
  • 1
    Don't forget [`std::numeric_limits::lowest()`](http://en.cppreference.com/w/cpp/types/numeric_limits/lowest). – rubenvb Sep 22 '14 at 07:51
0

I'm not sure I understand the problem; std::numeric_limited<double>::min() will return something like 2.2250738585072014e-308, which is a positive value. It's in scientific notation; the e-308 means you want to move the decimal place to the left about 308 times.

user1095108
  • 14,119
  • 9
  • 58
  • 116
NeomerArcana
  • 1,978
  • 3
  • 23
  • 50