random.3m (2010 09)
r
random(3M) random(3M)
NAME
random( ), srandom( ), initstate( ), setstate( ) - generate a pseudorandom number
SYNOPSIS
#include <stdlib.h>
long random(void);
void srandom(unsigned seed);
char *initstate(unsigned seed, char *state, size_t size);
char *setstate(char *state);
DESCRIPTION
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
0to2
31
-1. The period of this random-number generator is approximately 16 x (2
31
-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 2
69
.
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 gen-
erators. The initstate() function allows a state array, pointed to by the state argument, to be initial-
ized 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
init-
state() 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
initstate() function can be used, with the desired seed, state array, and size of the array.
The
setstate() function, with the desired state, can be used, followed by the srandom() func-
tion with the desired seed. The advantage of using both of these functions is that the size of the
state array does not have to be saved once it is initialized.
RETURN VALUE
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 pre-
vious state array. Otherwise, a NULL pointer is returned.
ERRORS
If the
setstate() function detects that the state information has been damaged, error messages are
written to standard error.
HP-UX 11i Version 3: September 2010 − 1 − Hewlett-Packard Company 1