Copyright © 1997 The Open Group

initstate, random, setstate, srandom - pseudorandom number functions

`#include <stdlib.h> char *initstate(unsigned int`

seed, char*state, size_tsize); long random(void); char *setstate(const char *state); void srandom(unsigned intseed);

Therandom()function uses a non-linear 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 2^{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

rand(),random()produces by default a sequence of numbers that can be duplicated by callingsrandom()with 1 as the seed.The

srandom()function initialises the current state array using the value ofseed.The

initstate()andsetstate()functions handle restarting and changing random-number generators. Theinitstate()function allows a state array, pointed to by thestateargument, to be initialised for future use. Thesizeargument, which specifies the size in bytes of the state array, is used byinitstate()to decide what type of 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. Other values greater than 8 bytes are rounded down to the nearest one of these values. For values greater than or equal to 8, or less than 32random()uses a simple linear congruential random number generator. Theseedargument specifies a starting point for the random-number sequence and provides for restarting at the same point. Theinitstate()function returns a pointer to the previous state information array.If

initstate()has not been called, thenrandom()behaves as thoughinitstate()had been called withseed=1 andsize=128.If

initstate()is called with 8 <=size<32, thenrandom()uses a simple linear congruential random number generator.Once a state has been initialised,

setstate()allows switching between state arrays. The array defined by thestateargument is used for further random-number generation untilinitstate()is called orsetstate()is called again. Thesetstate()function returns a pointer to the previous state array.

Ifinitstate()is called withsizeless than 8, it returns NULL.The

random()function returns the generated pseudo-random number.The

srandom()function returns no value.Upon successful completion,

initstate()andsetstate()return a pointer to the previous state array. Otherwise, a null pointer is returned.

No errors are defined.

None.

After initialisation, 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 bysrandom()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 initialised.

Although some implementations of

random()have written messages to standard error, such implementations do not conform to this specification.Issue 5 restores the historical behaviour of this function.

Threaded applications should use

rand_r(),erand48(),nrand48()orjrand48()instead ofrandom()when an independent random number sequence in multiple threads is required.

None.

drand48(),rand(),<stdlib.h>.

Copyright © 1997 The Open Group

[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]