include <stdlib.h>char *initstate(unsigned int seed, char *state, size_t size);
long random(void);
char *setstate(const char *state);
void srandom(unsigned int seed);
The size of the state array determines the period of the random-number generator. Increasing the state array size increases the period. With 256 bytes of state information, the period of the random number generator is greater than 2[69], which should be sufficient for most purposes.
Like rand(3C), random produces by default a sequence of numbers that can be duplicated by calling srandom with 1 as the seed. The srandom function initialises the current state array using the value of seed.
The initstate and setstate functions handle restarting and changing random-number generators. The initstate function allows a state array, pointed to by the state argument, to be initialised for future use. The size argument, which specifies the size in bytes of the state array, is used by initstate to decide what type of random-number generator to use; the larger the state array, the more random the numbers.
Values for the amount of state information are 8, 32, 64, 128, and 256 bytes. Other values greater than 8 bytes are rounded down to the nearest one of these values. For values smaller than 8, random uses a simple linear congruential random number generator.
The seed argument specifies a starting point for the random-number sequence and provides for restarting at the same point. The initstate function returns a pointer to the previous state information array. If initstate has not been called, then random behaves as though initstate had been called with a seed of 1 and size equal to 128. If initstate is called with a size less than 8, then random uses a simple linear congruential random number generator.
Once a state has been initialised, setstate allows switching between state arrays. The array defined by the state argument is used for further random-number generation until initstate is called or setstate is called again. The setstate function returns a pointer to the previous state array.
Using the setstate/srandom functions has the advantage that the size of the state array does not have to be saved once it is initialized.
random/srandom have almost the same calling sequence and initialization properties as rand/srand [see rand(3C)]. The difference is that rand produces a much less random sequence (though it can do so in less time than random/srandom). In fact, the low dozen bits generated by rand go through a cyclic pattern. All the bits generated by random are usable. For example,
random()&01
will produce a random binary value.
Unlike srand, srandom does not return the old seed because the amount of state information used is much more than a single word. Like rand, random will, by default, produce a sequence of numbers that can be duplicated by calling srandom with 1 as the seed.
Note that the state argument to initstate and setstate is assumed to be aligned as if it were an array of long (not char) elements. See the example below.
/* Initialize an array and pass it in to initstate. */static long state1[32] = { 3, 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b, 0x27fb47b9 };
main() { unsigned seed; int n;
seed = 1; n = sizeof(state1); initstate(seed, (char *)state1, n);
setstate((char *)state1); printf("%ld\n",random()); }