Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mersenne twister - is there a way to jump to a particular state?

I am a little unsure about the right forum for this question. It is between theoretical comp. sci./math and programming.

I use Mersenne-Twister to generate pseudo random numbers. Now, starting from a given seed, I would like to jump to the n-th number in the sequence.

I have seen this: http://www-personal.umich.edu/~wagnerr/MersenneTwister.html, and one scheme could be as follows:

Suppose, I need only the first N numbers in the complete random sequence from particular seed s.
I split the sequence in to p subsequences, march through all the N numbers, and save the state vector of the random number generator at the beginning of each subsequence.
Now to reach n-th number, I'll see that n falls in the k-th subsequence and I'll load the state vector for this subsequence and generate m consecutive random numbers where m-th number in k-th subsequence is the same as n-th number in the complete sequence ( n = m + (k-1) * N/p ).

But the state vector is 624 x 4 bytes long! I wonder if it is practically possible to jump to an arbitrary element in the mersenne-twister generated sequence.

like image 795
subhacom Avatar asked Nov 15 '10 12:11

subhacom


People also ask

Is Mersenne Twister truly random?

However, Excel uses what is called a pseudorandom number generator, in this case, the Mersenne Twister algorithm. What that means is that, technically speaking, the numbers that Excel's RAND functions generate aren't truly random.

What is the maximum number that you can get from the Mersenne Twister PRNG?

CUDA's implementation of the Mersenne Twister ( MT ) random number generator is limited to a maximal number of threads/blocks of 256 and 200 blocks/grid, i.e. the maximal number of threads is 51200 .

How does Mersenne Twister work?

you start with a seed (if you re-use the same seed you will obtain the same random numbers), you initialize it into a state . Then, every time you want to obtain a random number, you transform that state with a one-way function g . This is because you don't want people to find out the state out of the random output.

How does mt19937 work?

mt19937 stands for mersenne twister with a long period of 219937 – 1 which means mt19937 produces a sequence of 32-bit integers that only repeats itself after 219937 – 1 number have been generated.


2 Answers

Yes it is possible! It's called Jump Ahead.

You can find all the details to do this with Mersenne Twister on the homepage of MT's authors. Code is available as well as scientific publications explaining the algorithm:

http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/JUMP/index.html

like image 184
jopasserat Avatar answered Nov 15 '22 09:11

jopasserat


Today, there is a way to do this using discard. The complexity is linear in number of equivalent advances.

A working example:

  std::mt19937 engine(0);
  std::uniform_int_distribution<int> dis(0, 9);
  auto generator = std::bind(std::ref(dis), std::ref(engine));

  qDebug() << "First sequence: ";
  for (int i = 0; i < 20; i++) {
    qDebug() << "Random value: " << generator();
  }

  engine.seed(0); // reset
  engine.discard(15); // discard the first 15 numbers from the first sequence
  qDebug() << "Last five numbers from first sequence: ";
  for (int i = 0; i < 5; i++) {
    qDebug() << "Random value: " << generator();
  }
like image 27
oklar Avatar answered Nov 15 '22 10:11

oklar