pyCthon
pyCthon

Reputation: 12341

C++11/Boost Random libraries, starting the generation at a specified point in the cycle

Using the C++11 #include<random> or Boost C++ Boost.Random is it possible to use one seed to start the random number generator at an arbitrary sequence in that you can choose?.

In other words I want to be able to specific where in the sequence the number generator starts, while using the same seed.

For example if i'm using the mt19937 generator with a length of cycle 2^19937-1 I would like to start generating random numbers at a user specified position in the length of the cycle. Say I pick 1000, the generator will start at the 1000th position in the length of the cycle.

Upvotes: 2

Views: 224

Answers (1)

Howard Hinnant
Howard Hinnant

Reputation: 218780

Yes. There is a member discard(unsigned long long z) that does this for you. For example:

#include <random>
#include <cassert>

int main()
{
    std::mt19937 e1(6492);
    std::mt19937 e2(6492);
    const int N = 1000;
    for (int i = 0; i < N; ++i)
        e1();
    e2.discard(N);
    assert(e1() == e2());
}

This program should not assert.

Upvotes: 8

Related Questions