Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ srand(3C) — HP-UX 10.20

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

drand48(3C)

random(3M)

rand(3C)

NAME

rand(), rand_r(), srand() − simple random-number generator

SYNOPSIS

#include <stdlib.h>

int rand(void);

int rand_r(unsigned int *seed, int *randval);

void srand(unsigned int seed);

DESCRIPTION

rand() uses a multiplicative, congruential, random-number generator with period 232 that returns successive pseudo-random numbers in the range from 0 to 215−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. 

Reentrant Interfaces

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.  Note that rand() is a thread-safe function.  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 -1 and sets errno to EINVAL.  Otherwise, rand_r() returns 0. 

EXAMPLE

The following:

int x, y;

srand(10);
x = rand();
y = rand();

would produce the same results as:

int x, y, s = 10;

rand_r(&s, &x);
rand_r(&s, &y);

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 also note that the prototype of this function will change in the next release for conformance with the new POSIX Threads standard. 

SEE ALSO

drand48(3C), random(3M). 

STANDARDS CONFORMANCE

rand(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C

srand(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C

Hewlett-Packard Company  —  HP-UX Release 10.20:  July 1996

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026