HP-UX Reference (11i v1 05/09) - 3 Library Functions N-Z (vol 7)

r
rand(3C) rand(3C)
NAME
rand(), rand_r(), srand() - simple random-number generator
SYNOPSIS
#include <stdlib.h>
int rand(void);
int rand_r(unsigned int *seed);
void srand(unsigned int seed);
DESCRIPTION
rand() uses a multiplicative, congruential, random-number generator with period 2
32
that returns suc-
cessive pseudo-random numbers in the range from 0 to 2
15
1.
srand() can be called at any time to reset the random-number generator to a random starting point.
The generator is initially seeded with a value of 1.
rand_r() returns a random number at the address pointed to by the randval parameter. The seed
parameter can be set at any time to start the random-number generator at an arbitrary point.
APPLICATION USAGE
The
rand_r() interface has been provided to allow multiple threads to generate the same sequence of
random numbers concurrently.
RETURN VALUE
If seed or randval is NULL, rand_r() returns 0. Otherwise, rand_r() returns a psuedo-random
integer.
EXAMPLE
The following:
int x, y;
srand(10);
x = rand();
y = rand();
would produce the same results as:
int x, y, s = 10;
x=rand_r(&s);
y=rand_r(&s);
NOTE
The spectral properties of rand() leave a great deal to be desired. drand48() provides a much better,
though more elaborate, random-number generator (see drand48(3C)).
WARNINGS
Users of
rand_r() should note that rand_r() now conforms with POSIX.1c. The old prototype of
rand_r() is supported for compatibility with existing DCE applications only.
SEE ALSO
drand48(3C), random(3M), thread_safety(5).
STANDARDS CONFORMANCE
rand(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C
rand_r():POSIX.1c
srand(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C
Section 3776 Hewlett-Packard Company 1 HP-UX 11i Version 1: September 2005