dimanche 31 mai 2015

Random numbers generated using uniform real distribution in C++ are not really uniformly distributed

I wrote a small code to make sure that I can get random numbers from a really wide range, ex. [0, 10^36) because I am going to use these wide ranges later.

My code is as follows:

#include <iostream>
#include <cmath>
#include <random>
#include <chrono>

int main()
{   unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    double expo = pow(10,36);
    std::uniform_real_distribution<double> dist(0,expo);
    std::mt19937_64 rng(seed);
    for (int i=0; i<10; i++)
        std::cout << dist(rng) << std::endl;
    return 0;
}   

And the following is an example of the output:

6.75507e+035
4.01129e+035
6.85525e+035
8.85896e+035
3.1455e+035
3.04962e+035
5.48817e+035
3.54502e+035
2.24337e+035
2.23367e+035

As you can see, the random numbers are all really close to the upper endpoint of the given interval. I tried running the program lots of times, also increased 10 numbers to 100, but the random numbers were always close to the upper endpoint of the interval (with the exponent 35, and sometimes 34).

Since I have used std::uniform_real_distribution, I expect to have also and sometimes the numbers in the range [0, 1000] for example. I do not find this a uniform distribution. This is important to me that the random number is not only close to the upper endpoint because I am going to use the random number later in an if-statement:

if (random_number == 0)
    //do some operations

And the upper endpoint will be actually used as a rate, in which something occurs. But it seems that the random number has no chance to be zero sometimes.

I do not know why this happens and would really appreciate any idea or help.

(Eclipse 4.4.1, Windows 7)




Aucun commentaire:

Enregistrer un commentaire