Index Return to Main Contents

int random(void);

int srandom(
` `unsigned seed);

char *initstate(
` `unsigned seed,

` `char *state,

` `int size );

char *setstate (
` `char *state );

The following functions are supported in order to maintain backward compatibility
with previous versions of the operating system.
int random_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);

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.

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 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() 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 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 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() 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 once it is initialized.

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(), initstate_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.

The random() function returns a random number. The initstate() and setstate() functions return a pointer to the previous state information array.

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.

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.

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 corresponding value: The retval, rand_data, state, seed, or retval parameters are invalid, or the state field of the rand_data structure is invalid.

Functions: drand48(3), rand(3) delim off

- NAME
- LIBRARY
- SYNOPSIS
- PARAMETERS
- DESCRIPTION
- NOTES
- RETURN VALUES
- ERRORS
- RELATED INFORMATION

This document was created by man2html, using the manual pages.

Time: 02:41:39 GMT, October 02, 2010