Products
and Services Support
and Docs Training Partner Programs About Red
Hat
  download storecart
red hat logo
redhat.comredhat network

Red Hat Documentation: man pages: man3: Linux Programmer's Manual (3)

Manpage of RAND

RAND

Section: Linux Programmer's Manual (3)
Updated: 18 May 2025
Index  

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(), so the lower-order bits should be as random as the higher-order bits. However, on older rand() implementations, the lower-order bits are much less random than the higher-order bits.

In Numerical Recipes in C: The Art of Scientific Computing (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New York: Cambridge University Press, 1990 (1st ed, p. 207)), the following comments are made:

"If you want to generate a random integer between 1 and 10, you should always do it by

j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

and never by anything resembling

j=1+((int) (1000000.0*rand()) % 10);

(which uses lower-order bits)."

Random-number generation is a complex topic. The Numerical Recipes in C book (see reference above) provides an excellent discussion of practical random-number generation issues in Chapter 7 (Random Numbers).

For a more theoretical discussion which also covers many practical issues in depth, please see Chapter 3 (Random Numbers) in Donald E. Knuth's The Art of Computer Programming, volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts: Addison-Wesley Publishing Company, 1981.  

CONFORMING TO

SVID 3, BSD 4.3, ISO 9899  

SEE ALSO


 

Index

NAME
SYNOPSIS
DESCRIPTION
RETURN VALUE
NOTES
CONFORMING TO
SEE ALSO

This document was created by using the manual pages.
Time: 11:58:05 GMT, March 24, 2025

 

 


 
© 2001 Red Hat, Inc. All rights reserved.
About Red Hat  :  Legal statement  :  Privacy statement  :  Y2K statement  :  Contact Red Hat