问题
Please, help me understand this. After running the snippet:
random_device randomEngine;
mt19937 generatorEngine(randomEngine());
uniform_real_distribution<double> unifRandomValue(
numeric_limits<double>::min(),
numeric_limits<double>::max());
double test[1000];
for (int i{ 0 }; i < 1000; ++i) {
test[i] = unifRandomValue(generatorEngine);
}
Why is every generated value in the range [1.0E306, 1.8E308]
? I was expecting a random value uniformly distributed from near 0 to the max of double type.
Thanks in advance!
Here is a more complete example :
const size_t size{ 1000 };
std::random_device randomEngine;
std::mt19937 generatorEngine(randomEngine());
std::uniform_real_distribution<double> unifRandomValue(
std::numeric_limits<double>::min(),
std::numeric_limits<double>::max());
std::array<double, size> test;
for (int i{ 0 }; i < size; ++i) {
test[i] = unifRandomValue(generatorEngine);
}
auto minMaxIt = std::minmax_element(test.begin(), test.end());
// average without overflow
double average{ 0.0 };
for (int i{ 0 }; i < size; ++i) {
average += test[i] / size;
}
std::cout << "min value : " << *minMaxIt.first << std::endl;
std::cout << "max value : " << *minMaxIt.second << std::endl;
std::cout << "average : " << average << endl;
// one possible output (they are all similar)
// min value : 1.73361e+305
// max value : 1.79661e+308
// average : 8.78467e+307
回答1:
Well, that's the property of a uniform generator:
90% of values will be in the highest order of magnitude you specify.
Think smaller; consider the integer range 0 to 99 inclusive: 90% of the drawings will have 2 digits.
来源:https://stackoverflow.com/questions/46771440/uniform-real-distribution-is-not-uniform