Table of Contents

NAME

rand, srand - random number generator.

SYNOPSIS

#include <stdlib.h>
int rand(void);
void srand(unsigned int seed);

DESCRIPTION

The rand() function returns a pseudo-random integer between 0 and RAND_MAX.

The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value.

If no seed value is provided, the rand() function is automatically seeded with a value of 1.

RETURN VALUE

The rand() function returns a value between 0 and RAND_MAX. The srand() returns no value.

NOTES

The versions of rand() and srand() in the Linux C Library use the same random number generator as random() and srandom().

To ensure a good distribution for a subrange of values, use code like the below:
i = RAND_MAX / my_range
i *= my_range
while ((j = rand()) >= i) continue;
return j % i;
(code example based on code from Karl Lehenbauer's fortune cookie program, which credits Ken Arnold, Unix Review, October 1987).

CONFORMING TO

SVID 3, BSD 4.3, ISO 9899

SEE ALSO

random(3), srandom(3), initstate(3), setstate(3)


Table of Contents


www.fiveanddime.net


Google
Web www.fiveanddime.net