This section describes a set of random number generation functions that are derived from BSD. There is no advantage to using these functions with the GNU C library; we support them for BSD compatibility only.
The prototypes for these functions are in `stdlib.h'.
0
to RAND_MAX
.
Note: Historically this function returned a long
int
value. On 64bit systems long int
would have been larger
than programs expected, so random
is now defined to return
exactly 32 bits.
srandom
function sets the state of the random number
generator based on the integer seed. If you supply a seed value
of 1
, this will cause random
to reproduce the default set
of random numbers.
To produce a different set of pseudo-random numbers each time your
program runs, do srandom (time (0))
.
initstate
function is used to initialize the random number
generator state. The argument state is an array of size
bytes, used to hold the state information. It is initialized based on
seed. The size must be between 8 and 256 bytes, and should be a
power of two. The bigger the state array, the better.
The return value is the previous value of the state information array.
You can use this value later as an argument to setstate
to
restore that state.
setstate
function restores the random number state
information state. The argument must have been the result of
a previous call to initstate or setstate.
The return value is the previous value of the state information array.
You can use this value later as an argument to setstate
to
restore that state.
Go to the first, previous, next, last section, table of contents.