# How to determine how many random numbers were generated, using Mersenne twister?

6 views (last 30 days)

Show older comments

I want to use Mersenne Twister to generate 2 sets of random numbers using common seed, and want to determine which of these sets used more random numbers. Random number generation is performed by a called function; so calling function doesn't directly know how many random numbers were generated in each call.

rng_state_initial = rng;

% Generate 1st sequence

[output1] = joeblow1(...);

% save the state of rng after 1st sequence generated

rng_state_after_1st_sequence = rng;

% Generate 2nd sequence

rng(rng_state_initial);

[output2] = joeblow2(...);

% save the state of rng after 2nd sequence generated

rng_state_after_2nd_sequence = rng;

If I want to then generate additonl lrandom numbers indepedent of 1st and 2nd sequences, should I use

rng(rng_state_after_1st_sequence)

or

rng(rng_state_after_2nd_sequence)

before generating the new random numbers?

I don't know which random number state (1st or 2nd sequence) is further along.

I can do a hack job to do either rng(rng_state_after_1st_sequence) or rng(rng_state_after_2nd_sequence) and then generate a ginormous number of random numbers that I'm sure exceeds the number of random numbers generated in 1st or 2nd sequence,, save the state after that, and then generate the new random numbers. But I don't want to do that.

Note that multiple streams are not available with Mersenne Twister.

##### 2 Comments

### Accepted Answer

Walter Roberson
on 26 Nov 2022

Record the two rng states. Loop generating one number at a time from each of the two streams until the newly generated state of one of them equals the recorded state of the other.

There is no iteration count information recorded in the state.

If I recall correctly, about two years ago I saw an abstract for a paper about recovering rng state. If you generate N random numbers, can you deduce what the rng state must have been? The number required was much smaller than you might expect. I do not recall the details now, but my brain is saying "Wasn't it only in the range of 8 to 13 needed?" I did not read the paper to see what techniques they were using, but I wonder if some of what they did might be useful for figuring out the meaning of the state differences?

### More Answers (1)

Jan
on 26 Nov 2022

Edited: Jan
on 26 Nov 2022

The direct solution is to modify the function joeblow1 and joeblow1 such, that they count the created random numbers.

Another option is to use a wrapper function, which calls the random number generation and counts the number of created values.

Using the state of the random number generator to count the produced values is rather indirect and cannot be efficient. I cannot imagine a purpose to create additional random numbers only to set the rng to a defined state. Using a new state would be much cheaper. Therefore I assume, that this is an XY-problem. Please explain, which problem you want to solve actually.

"Note that multiple streams are not available with Mersenne Twister." - is this really the case?

##### 4 Comments

Jan
on 27 Nov 2022

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!