Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uniform_real_distribution Is Not Uniform

Tags:

c++

random

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
like image 555
Aesope Avatar asked Oct 16 '17 13:10

Aesope


1 Answers

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.

like image 199
Bathsheba Avatar answered Nov 11 '22 09:11

Bathsheba