|HP-UX Reference > R
HP-UX 11i Version 3: February 2007
random(), srandom(), initstate(), setstate() — generate a pseudorandom number
void srandom(unsigned seed);
char *initstate(unsigned seed, char *state, size_t size);
char *setstate(char *state);
The random() and srandom() functions are random-number generators that have virtually the same calling sequence and initialization properties as the rand() and srand() functions, but produce sequences that are more random. The low 12 bits generated by the rand() function go through a cyclic pattern, while all the bits generated by the random() function are usable. For example, random() & 01 produces a random binary value.
The random() function uses a nonlinear additive feedback random-number generator employing a default state array size of 31 long integers to return successive pseudorandom numbers in the range from 0 to 231-1. The period of this random-number generator is approximately 16 x (231-1). 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 269.
Like the rand() function, the random() function produces by default a sequence of numbers that can be duplicated by calling the srandom() function with a value of 1 as the seed.
The srandom() function initializes 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 initialized for future use. The size argument, which specifies the size in bytes of the state array, is used by the initstate() function to decide how sophisticated a 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. Amounts less than 8 bytes return a NULL pointer, while other amounts are rounded down to the nearest known value. 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.
Once a state has been initialized, the setstate() function allows switching between state arrays. The array defined by the state argument is used for further random-number generation until the initstate() function is called or the setstate() function is called again. The setstate() function returns a pointer to the previous state array.
After initialization, a state array can be restarted at a different point in one of two ways:
The random() function returns the generated pseudorandom number.
The srandom() function returns no value.
Upon successful completion, the initstate() and setstate() functions return a pointer to the previous state array. Otherwise, a NULL pointer is returned.
If the setstate() function detects that the state information has been damaged, error messages are written to standard error.