Home > Mobile >  Does the POSIX initstate() function take into account the data in the passed state array?
Does the POSIX initstate() function take into account the data in the passed state array?

Time:03-19

char *initstate(unsigned int seed, char *state, size_t n);

Before calling initstate(), should I fill the state[] array with some data? Does the initstate function ignore the whole content of the state array? Do only the provided size and seed matter? Is the purpose of initstate to fill the state[] array without taking into account its current content?

I have seen at least 1 example on the net, filling the state[] array before initstate call. -- I cannot find the url, sorry. -- But, my experience shows that as long as the seed remains the same, the (previous) content of the state[] array does not matter. In terms of successive random numbers yielded by the random() function.

CodePudding user response:

The initstate() function "allows a state array state to be initialized for use by random()". In other words, initstate() will write a new state into the passed state buffer and then set it as the current internal state which will be used (and updated) by subsequent random() calls. This can also be seen in the glibc source. On the other hand, the setstate() function takes an already initialized state and sets it as internal state as is.

Is the purpose of initstate to fill the state[] array without taking into account its current content?

Yep, correct.

I have seen at least 1 example on the net, filling the state[] array before initstate call. [...] my experience shows that as long as the seed remains the same, the (previous) content of the state[] array does not matter.

You're right, that is unneeded. The data in the array doesn't matter and will be overwritten.

  • Related