Largest Seed Possible for Mersenne Twister c++

513 views Asked by At

I would like to find out the largest value I can seed a random number generator in c++. My code is as follows:

mt19937 myRandomGenerator(seed);

How large can the variable, seed be? I have noticed that if the value becomes too large, the random number generator spits out the same sequence of 'random' numbers. I would like to know how large the seed can be without this happening.

2

There are 2 answers

0
Andrew Tomazos On

seed is std::uint_fast32_t which is usually just a 32-bit int. Every value in the range [0..2^32) should produce different results. If you are seeing the same sequence from two different seed values, then you are either making an observational error and the seed you are inputing is actually the same, or there is a bug in your standard library implementation.

Prepare a short self-contained test program demonstrating the misbehaviour and post it here.

0
Howard Hinnant On

The wording of the question is somewhat ambiguous. It is possible to read the question as asking about this behavior:

#include <random>
#include <iostream>

int
main()
{
    std::mt19937 eng1(4294967296);
    for (int i = 0; i < 10; ++i)
        std::cout << eng1() << '\n';
    std::cout << '\n';
    std::mt19937 eng2(0);
    for (int i = 0; i < 10; ++i)
        std::cout << eng2() << '\n';
}

My compiler spits out a warning which should be heeded:

test.cpp:7:23: warning: implicit conversion from 'long' to 'result_type' (aka 'unsigned int') changes value from 4294967296 to 0 [-Wconstant-conversion]
    std::mt19937 eng1(4294967296);
                 ~~~~ ^~~~~~~~~~
1 warning generated.

If this is what you are seeing it is because

static_cast<std::mt19937::result_type>(4294967296) == 0

If this is not the behavior you're asking about, please clarify your question.