random(3) Library Functions Manual random(3)
NAME
random, random_r, srandom, srandom_r, initstate, initstate_r, setstate, setstate_r - Generate pseudo-random number
LIBRARY
Standard C Library (libc.a)
SYNOPSIS
int random(void);
int srandom( unsigned seed);
char *initstate( unsigned int seed, char *state, size_t size);
char *setstate( const char *state);
The following functions are supported in order to maintain backward compatibility with previous versions of the operating system. int ran-
dom_r( int *retval, struct random_data *rand_data);
int srandom_r( unsigned seed, struct random_data *rand_data);
int initstate_r( unsigned seed, char *state, int size, char **retval, struct random_data *rand_data);
int setstate_r( char *state, char **retval, struct random_data *rand_data);
PARAMETERS
Specifies an initial seed value. Points to the array of state information. Specifies the size of the state information array. Points to
a place to store the random number. Points to a random_data structure.
DESCRIPTION
The random() and srandom() functions are random number generators that have virtually the same calling sequence and initialization proper-
ties as the rand() and srand() functions, but produce sequences that are more random. The low 12 bits generated by rand go through a
cyclic pattern. All bits generated by random 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 integers to
return successive pseudo-random numbers in the range from 0 to (2^31)-1. The period of this random number generator is approximately
16*((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 a full 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 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, unlike the srand() function, does not return the old seed because the
amount of state information used is more than a single word.
The initstate() and setstate() functions handle restarting and changing random-number generators. The initstate() function allows a state
array, passed in as an argument, to be initialized for future use. The size in bytes of the state array is used by the initstate() func-
tion 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 are rounded down to the nearest known value. The seed parameter
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 rapid switching between states. The array defined by the state parameter
is used for further random-number generation until the initstate() function is called or the setstate() function is called again. The set-
state() 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 sran-
dom() function 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 after it is initialized.
NOTES
The random_r(), srandom_r(), initstate_r(), and setstate_r() functions are the reentrant versions of the random(), srandom(), initstate(),
and setstate() functions. They are supported in order to maintain backward compatibility with previous versions of the operating system.
Upon successful completion, the initstate_r() and setstate_r() functions provide a pointer to the returned state in retval. The random_r()
function provides a pointer to the returned random number in retval. Upon successful completion, the random_r(), srandom_r(), init-
state_r(), and setstate_r() functions return a value of 0 (zero). Upon error, they return a value of -1 and may set errno.
Note that the srandom_r() function takes the rand_data structure, which should first be initialized by the initstate_r() function. Note
also that the rand_data.state parameter needs to be NULL before the initstate_r() or setstate_r() functions are called.
RETURN VALUES
Upon successful completion, the random() function returns a random number.
Upon successful completion, the initstate() and setstate() functions return a pointer to the previous state information array. Upon error,
a value of 0 (zero) is returned. If initstate() is called with size less than 8, NULL is returned.
Upon successful completion, the srandom() function returns success with a value of 0 (zero). Upon failure, it returns -1 and may set
errno. The srandom() function initializes the state seed.
ERRORS
If the setstate() function detects that the state information has been damaged, an error message is written to standard error.
If any of the following conditions occurs, the random_r(), srandom_r(), setstate_r(), and initstate_r() functions set errno to the corre-
sponding value: The retval, rand_data, state, or seed parameters are invalid, or the state field of the rand_data structure is invalid.
RELATED INFORMATION
Functions: drand48(3), rand(3) delim off
random(3)