3

I'm playing a bit with c++ random engines, and something upsets me. Having noticed that the values I had were roughly of the same order, I did the following test:

#include <random>
#include <functional>
#include <iostream>

int main()
{
    auto res = std::random_device()();
    std::ranlux24 generator(res);
    std::uniform_int_distribution<uint32_t> distribution;
    auto roll = std::bind(distribution, generator);


    for(int j = 0; j < 30; ++j)
    { 
        double ssum = 0;
        for(int i = 0; i< 300; ++i)
        {
            ssum += std::log10(roll());
        }
        std::cout << ssum / 300. << std::endl;
    }
    return 0;
}

and the values I printed were all about 9.2 looking more like a normal distribution, whatever the engine I used. Is there something I have not understood correctly? Thanks, Guillaume

  • 1
    Why are you taking `log10`? Isn't that going to dramatically affect the shape of the distribution? – Chris Beck Jul 05 '16 at 14:18
  • 2
    Depending on the generator you use, you may need more than a single call to `random_device` to properly seed all the internal state. For example, `std::mt19937` uses 624bytes of internal state, so you'd need a `std::seed_seq` and multiple inputs to seed all of it. See for example http://stackoverflow.com/questions/15509270/does-stdmt19937-require-warmup – Jesper Juhl Jul 05 '16 at 14:21
  • 7
    see also the [central limit theorem](https://en.wikipedia.org/wiki/Central_limit_theorem). You're *averaging* the results of a random number generator. "[The] arithmetic mean of a sufficiently large number of iterates of independent random variables, each with a well-defined (finite) expected value and finite variance, will be approximately normally distributed, regardless of the underlying distribution." You're taking the mean on *300* randomly distributed independent variables. – jaggedSpire Jul 05 '16 at 14:21

1 Answers1

14

Having noticed that the values I had were roughly of the same order

This is exactly what you'd expect with a uniform random number generator. There are 9 times as many integers in the range [10^(n-1),10^n) as there are in the range [0,10^(n-1)).

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274