简体   繁体   中英

What's the difference between a random number generator's seed and state?

I saw a C++ program accepting a seed and a state to setup a std::default_random_engine , which is a typedef to std::linear_congruential_engine (on my system at least). The seed() method is used to set the initial seed and operator>> for the state.

I'm aware of the principle of seeding random number generators (RNG), but used it interchangeable with its state.

The seed is the value used to initialise the generator, the state is the current state of the generator after each call to generate a random number. For very simple random number generators, such as linear congruential ones, the seed and the state are the same thing (or at least, are stored in the same variable), but they certainly don't have to be.

If you (re-)seed a PRNG, thus (re-)initializing it, you replace its current state with a new one which is a (possibly trivial) function of the seed. This initial function is often more complex to distribute the entropy over all the state, in an effort to alleviate patterns in the input.

Restoring the internal state with operator>> uses such a trivial mapping.

Whichever (re-)seeding is done last is effective, the rest just being wasted effort.

Disclaimer: I am not an expert regarding the theoretical aspect of random number generators, most of the content below actually comes from the C++ standard itself.

The state X i of a generator, is the actual internal state of a generator - the (minimal) information you need to generate the next state and the next value of the generator:

  • you transition from one state to another using a "transition" function, X i+1 = TA(X i ) ;
  • you generate a value by using a "generation" function, GA(X i ) .

The seed S is a "value" used to generate the first state X 0 .

The state of a generator is inherent to it (see below), two implementations of the same generator will have the same state (in a theoretical sense), but may require completely different seeds.

In C++, you seed a Mersenne Twister engine using a single value, while its state is a sequence of integers... I could choose to implement a Mersenne Twister engine that could only be seed by a sequence of integer (that would become the first state).


Some examples from the standard random library to better understand:

A linear congruential generator is simply a generator that follows a recursive relation of the form:

X i+1 = TA(X i ) = (a . X i + c) mod m

Where a , c and m are parameters of the generator. In this case:

  • the state of the engine is simply the current value of X i ;
  • the seed is the value of X 0 ;

...when you seed the generator with a value k , you actually simply set X i = k .

But there are other generators, eg in the C++11 standard random library, you will find Mersenne Twister (MT) generators.

I am certainly not an expert on Mersenne Twister generator, but from the c++ draft , you can see that:

  • the state of a MT generator is actually a sequence of integer X i = (X i,0 , X i, 1 , ...) ;
  • the seed for a MT (in the standard) is actually a single value;

...when you seed the generator with a value k , you actually perform "complex" operations 1 to compute X 0 from k , but X 0 is certainly not k .

1 I will not go into details for these operations, because I do not really know them, but you can look at the standard to see how the state (sequence) is generated from the seed.

Typically, a seed value is used to generate random numbers. Often for performance reasons, the seed value is used to generate a set of numbers, which are then added to an array or memory table. Further calls to fetch random numbers simply read pre-generated values from the table. The current index into the table and the current set of values in the table can be considered the state. When working with complex systems that can utilize a lot of call for random numbers (An example would be a game engine) debugging (reproducing bugs) becomes very difficult unless you can reproduce "random" events. If you save and then restore the "state" and seed value you can ensure that when the code runs it will choose the same "random" numbers each time. I'm sure that is just one of many possible answers, but hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM