简体   繁体   中英

c++11 random doubles using uniform_real_distribution

So I am trying to get uniform random double s 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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM