Generate always same sequence of random numbers

Hi!

I'm using the uniform_int_distribution and uniform_real_distribution along with the std::mt19937 engine seeded with random_device.

If I wanted to generate always the same sequence of random numbers, how should I seed the std::mt19937 engine? Or should I use another enginee?

Thanks!
> If I wanted to generate always the same sequence of random numbers,
> how should I seed the std::mt19937 engine?

Don't seed it explicitly; all default constructed std::mt19937 objects generate the same sequence of pseudo random numbers.

Alternatively, seed it with the same value during each run.
Ok, thanks!

How do I know which seed has been used?

Suppose I've the following generator which I seeded with a number X

std::mt19937 gen(X);

I thought I could simply use gen() to know which seed I've used (i.e. X) or, in general, if I don't seed the generator, which default seed has been used, but it seemed to return a different seed than X. Is it possible? Why? Or maybe I simply did a mistake?

I've no experience on how to create or, simply, how a pseudo-random number generator really works under the hood (I should inform myself).

My question is: if I use the same seed, am I assured that I will always have the same sequence in the same order, or am I assured only that the output numbers will be the same but possibly in different orders?

I know that a sequence usually means that order counts, otherwise it would be called a set, but I just want to make sure.
Last edited on
> if I use the same seed, am I assured that I will always have the same sequence in the same order

Yes.

1
2
3
4
    // rng_a, rng_b and rng_c will generate the same sequence of numbers
    std::mt19937 rng_a ; // default constructed (the IS specifies that the default seed is 5489U)
    std::mt19937 rng_b( 5489U ) ; // constructed with seed 5489U
    std::mt19937 rng_c( 5489U ) ; // constructed with same seed as rng_b 

Snippet: http://coliru.stacked-crooked.com/a/1367c4bd2c94f63f
Topic archived. No new replies allowed.