Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

drand48(3)

rand(3)

random(3)  —  Subroutines

OSF

NAME

random, srandom, initstate, setstate − Generates better pseudo-random numbers

LIBRARY

Standard C Library (libc.a)

SYNOPSIS

long random ( void ); srandom (
int seed ); char ∗initstate (
unsigned seed,
char ∗state,
int size ); char ∗setstate (
char ∗state );

PARAMETERS

seedSpecifies an initial seed value. 

statePoints to the array of state information. 

sizeSpecifies the size of the state information array. 

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 dozen bits generated by the rand() function go through a cyclic pattern, and 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 pseudo-random 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 a full 256 bytes of state information, the period of the random-number generator is greater than 269, 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 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 less than 8 bytes generate an error, while other 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:

     1.The initstate() function can be used, with the desired seed, state array, and size of the array. 

     2.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. 

RETURN VALUES

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

ERRORS

If the initstate() function is called with less than 8 bytes of state information, or if the setstate() function detects that the state information has been damaged, error messages are sent to the standard output. 

RELATED INFORMATION

Functions: drand48(3), rand(3)

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026