Search code examples
c++c++11randomstdarray

How to detect that the entire period of C++ random engine has been consumed


I want to write a small (fast) C++ program which essentially "detects" that the entire period of a std::minstd_rand0 engine has been consumed. In other words I want to detect when the sequence is repeating (and also measure the time required for the sequence to repeat). I don't care about the target distribution (it can be uniform).

Any ideas on how I should proceed? One option I was considering is to create two std::array variables. In the first std::array I would store say the 10000 first pseudo-random variates returned by std:minstd_rand0. I would then proceed by filling the other std::array with successive blocks of 10000 variates and compare the contents of the 2 arrays after each pass of 10000 variates. I would consider that the entire period has been consummed once the 2 arrays are equals.

Would this approach be sensible?


Solution

  • Standard random number engines can be compared to each other--they compare equal if and only if they have the same state.

    Therefore, you can pretty easily measure the period with code that:

    1. Creates two default constructed generators
    2. Executes a loop generating a number from one of the generators, and incrementing a count.
    3. Until the two generators compare equal again.

    At least in my quick test of std::minstd_rand0, I get a result of

     2147483646
    

    Needless to say, this is a lot more practical with std::minstd_rand0 than with std::mt19937 (for one obvious example).