[ Previous | Next | Contents | Glossary | Home | Search ]
AIX Version 4.3 Base Operating System and Extensions Technical Reference, Volume 2

random, srandom, initstate, or setstate Subroutine


Generates pseudo-random numbers more efficiently.


Standard C Library (libc.a)


#include <stdlib.h>
long random ( )
void srandom (Seed)
unsigned int Seed;
char *initstate (Seed, State, Number)
unsigned int Seed;
char *State;
size_t Number;
char *setstate (State)
const char *State;


Attention: Do not use the random, srandom, initstate, or setstate subroutine in a multithreaded environment.

The random subroutine 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 very large, 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.

With a full 256 bytes of state information, the period of the random-number generator is greater than 2**69, which should be sufficient for most purposes.

The random and srandom subroutines have almost the same calling sequence and initialization properties as the rand and srand subroutines. The difference is that the rand subroutine produces a much less random sequence; in fact, the low dozen bits generated by the rand subroutine go through a cyclic pattern. All the bits generated by the random subroutine are usable. For example, random( )&01 produces a random binary value.

The srandom subroutine, unlike the srand subroutine, does not return the old seed because the amount of state information used is more than a single word. The initstate subroutine and setstate subroutine handle restarting and changing random-number generators. Like the rand subroutine, however, the random subroutine by default produces a sequence of numbers that can be duplicated by calling the srandom subroutine with 1 as the seed.

The initstate subroutine allows a state array, passed in as an argument, to be initialized for future use. The size of the state array (in bytes) is used by the initstate subroutine, to decide how sophisticated a random-number generator it should use; the larger the state array, the more random are the numbers. Values for the amount of state information are 8, 32, 64, 128, and 256 bytes. For amounts greater than or equal to 8 bytes, or less than 32 bytes, the random subroutine uses a simple linear congruential random number generator, 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 subroutine returns a pointer to the previous state information array.

Once a state has been initialized, the setstate subroutine allows rapid switching between states. The array defined by State parameter is used for further random-number generation until the initstate subroutine is called or the setstate subroutine is called again. The setstate subroutine 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:


Seed Specifies an initial seed value.
State Points to the array of state information.
Number Specifies the size of the state information array.

Error Codes

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

Implementation Specifics

These subroutines are part of Base Operating System (BOS) Runtime.

Related Information

The drand48, erand48, jrand48, lcong48, lrand48, mrand48, nrand48, seed48, or srand48 subroutine, rand or srand subroutine.

Subroutines Overview in AIX General Programming Concepts: Writing and Debugging Programs.

[ Previous | Next | Contents | Glossary | Home | Search ]