Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

random

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)

like image 349
azish Avatar asked May 31 '15 18:05

azish


People also ask

Are random numbers uniformly distributed?

To be precise, the random numbers are uniformly distributed, which means that all numbers in the range are equally probable, and greater than or equal to zero and less than one. It doesn't matter whether the numbers are true hardware-generated random values or a pseudorandom sequence generated by a computer.

What is non uniformly distributed random number?

Non-uniform random variate generation or pseudo-random number sampling is the numerical practice of generating pseudo-random numbers (PRN) that follow a given probability distribution. Methods are typically based on the availability of a uniformly distributed PRN generator.

Is rand () uniform distribution?

The rand function generates arrays of random numbers whose elements are uniformly distributed in the interval ( 0 , 1 ).


1 Answers

As you can see, the random numbers are all really close to the upper endpoint of the given interval.

No, they're not. This one, for example:

2.23367e+035

Note that in the range [0, 1e36], the sub-range [1e35, 1e36] is 9 times as large as the sub-range [0, 1e35], so with a uniform distribution, you can expect to see those numbers 9 times as often. You will see numbers with an exponent of 34 now and then, but exponents any lower get exceedingly rare.

like image 87
Benjamin Lindley Avatar answered Oct 05 '22 09:10

Benjamin Lindley