RAND(3c,L) AIX Technical Reference RAND(3c,L)
-------------------------------------------------------------------------------
rand, srand
PURPOSE
Generates pseudo-random numbers.
LIBRARY
Standard C Library (libc.a)
Berkeley Compatibility Library (libbsd.a)
SYNTAX
int rand ( ) void srand (seed);
unsigned int seed;
DESCRIPTION
The rand subroutine generates random numbers using a multiplicative
congruential algorithm. The random-number generator has a period of 2(32), and
it returns successive pseudo-random numbers in the range from 0 to 2(15) - 1.
The srand subroutine resets the random-number generator to a random starting
point. The generator is initially seeded with a value of 1.
Note: The rand subroutine is a very simple random-number generator. Its
spectral properties, the mathematical measurement of how "random" the
number sequence is, are somewhat weak. See "drand48" or "random,
srandom, initstate, setstate" for a more elaborate random-number
generator that has better spectral properties.
COMPATIBILITY NOTE
The 4.3BSD version of rand returns a number in the range 0 to 2(31)-1, rather
than 0 to 2(15)-1, and can be used by compiling with the Berkeley Compatibility
Library (libbsd.a). There are better random number generators, as noted above,
however rand and srand are the interfaces defined for the ANSI C Library. The
following functions define the semantics of rand and srand, and are included
here to facilitate porting applications from different implementations:
Processed November 7, 1990 RAND(3c,L) 1
RAND(3c,L) AIX Technical Reference RAND(3c,L)
static unsigned int next = 1;
int rand ( )
{
next = next * 1103515245 + 12345;
return ( (next/65536) % 32768);
}
void srand (seed)
int seed;
{
next = seed;
}
Processed November 7, 1990 RAND(3c,L) 2