Caution: This version of this document is no longer maintained. For the latest documentation, see


Set the seed for a pseudo-random number generator


#include <stdlib.h>

void srandom( unsigned int seed );


The seed of the sequence of pseudo-random integers.



Use the -l c option to qcc to link against this library. This library is usually included automatically.

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


The srandom() function initializes the current state array using the value of seed.

Use this function in conjunction with the following:

Initialize the state of the pseudo-random number generator.
Generate a pseudo-random number using a default state.
Specify the state of the pseudo-random number generator.

The random() and srandom() functions have (almost) the same calling sequence and initialization properties as rand() and srand() 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 269.

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

After initialization, a state array can be restarted at a different point in one of two ways:


POSIX 1003.1 XSI

Cancellation point No
Interrupt handler No
Signal handler No
Thread No

See also:

drand48(), initstate(), rand(), random(), setstate(), srand()