Search code examples
cfunctionrandommersenne-twister

Mersenne Twister init_by_array() function clarification


I trying to implement cross-platform consistent random number generation with a 32-bit seed. Most post point me to Mersenne Twister or writing my own implementation.

In the source code there is a function called void init_by_array().

Is the only purpose of this function to extend the seed passed 32-bit?

From the ReadMe

init_by_array(init_key, key_length) initializes the state vector by using an array init_key[] of unsigned 32-bit integers of length key_kength. If key_length is smaller than 624, then each array of 32-bit integers gives distinct initial state vector. This is useful if you want a larger seed space than 32-bit word.

Failing to understand this just assuming based on the last sentence.

The code seems to run fine only using init_genrand() and seems to be producing consistent results.


Solution

  • Mersenne Twister has 19937 bits of state space that it uses to iterate through the sequence of values it produces. If you initialize it with a 32 bit integer, you are restricting it to just 232 out of the 219937 possible starting points, and there are a massive number of sample trajectories that you will never see. The init_by_array() function allows you to specify more bits for the initial state, giving the potential to achieve any of the sampling trajectories which MT is capable of generating.