*Generate a pseudo-random number from the default state*

#include <stdlib.h> long random( void );

`libc`

Use the ` -l c` option to

This function is in libc.a, but not in libc.so
(in order to save space). |

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

Use this function in conjunction with the following:

*initstate()*- Initialize the state of the pseudo-random number generator.
*setstate()*- Specify the state of the pseudo-random number generator.
*srandom()*- Set the seed used by the pseudo-random number generator.

The *random()* and *srandom()* functions have
(almost) the same calling sequence and initialization properties as
*rand()*
and
*srand()*
The difference is that *rand()*
produces a much less random sequence. In fact, the low dozen
bits generated by *rand()* go through a cyclic pattern. All the
bits generated by *random()* are usable. For example,

random() & 01

produces a random binary value.

Unlike *srand()*, *srandom()* doesn't return
the old seed
because the amount of state information used is much more
than a single word. The *initstate()* and *setstate()*
routines are provided to deal
with restarting/changing random number generators. 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 calling *srandom()*
with 1 as the seed.

If *initstate()* hasn't been called, *random()* behaves as
though *initstate()* had been called with *seed*=1 and
*size*=128.

If *initstate()* is called with *size* less than 8,
*random()* uses a
simple linear congruential random number generator.

The generated pseudo-random number.

See *initstate()*.

Safety: | |
---|---|

Cancellation point | No |

Interrupt handler | No |

Signal handler | Yes |

Thread | No |

*drand48()*,
*initstate()*,
*rand()*,
*setstate()*,
*srand()*,
*srandom()*