1RAND(3)                    Linux Programmer's Manual                   RAND(3)
2
3
4

NAME

6       rand, rand_r, srand - pseudo-random number generator
7

SYNOPSIS

9       #include <stdlib.h>
10
11       int rand(void);
12
13       int rand_r(unsigned int *seedp);
14
15       void srand(unsigned int seed);
16

DESCRIPTION

18       The  rand()  function  returns  a  pseudo-random  integer between 0 and
19       RAND_MAX.
20
21       The srand() function sets its argument as the seed for a  new  sequence
22       of  pseudo-random  integers  to be returned by rand().  These sequences
23       are repeatable by calling srand() with the same seed value.
24
25       If no seed value is provided,  the  rand()  function  is  automatically
26       seeded with a value of 1.
27
28       The function rand() is not reentrant or thread-safe, since it uses hid‐
29       den state that is modified on each call. This might just  be  the  seed
30       value to be used by the next call, or it might be something more elabo‐
31       rate. In order to get reproducible behaviour in a threaded application,
32       this  state  must  be  made explicit. The function rand_r() is supplied
33       with a pointer to an unsigned int, to be used as state.  This is a very
34       small  amount  of  state, so this function will be a weak pseudo-random
35       generator. Try drand48_r(3) instead.
36

RETURN VALUE

38       The rand()  and  rand_r()  functions  return  a  value  between  0  and
39       RAND_MAX.  The srand() function returns no value.
40

EXAMPLE

42       POSIX.1-2001 gives the following example of an implementation of rand()
43       and srand(), possibly useful when one needs the same  sequence  on  two
44       different machines.
45
46           static unsigned long next = 1;
47
48           /* RAND_MAX assumed to be 32767 */
49           int myrand(void) {
50               next = next * 1103515245 + 12345;
51               return((unsigned)(next/65536) % 32768);
52           }
53
54           void mysrand(unsigned seed) {
55               next = seed;
56           }
57

NOTES

59       The  versions of rand() and srand() in the Linux C Library use the same
60       random number generator as random() and srandom(), so  the  lower-order
61       bits should be as random as the higher-order bits.
62
63       Most  modern rand() functions, including the one provided on Linux sys‐
64       tems, do not suffer from this problem, so using expressions like
65              rand() % 10
66       works fine.
67
68       However, on older rand() implementations, and  on  current  implementa‐
69       tions  on  different systems, the lower-order bits are much less random
70       than the higher-order bits.  Do not use this function  in  applications
71       intended to be portable when good randomness is needed.
72
73       In  Numerical Recipes in C: The Art of Scientific Computing (William H.
74       Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New
75       York:  Cambridge University Press, 1992 (2nd ed., p. 277)), the follow‐
76       ing comments are made:
77              "If you want to generate a random integer between 1 and 10,  you
78              should always do it by using high-order bits, as in
79
80                     j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0)));
81
82              and never by anything resembling
83
84                     j = 1 + (rand() % 10);
85
86              (which uses lower-order bits)."
87
88       Random-number  generation is a complex topic.  The Numerical Recipes in
89       C book (see reference above) provides an excellent discussion of  prac‐
90       tical random-number generation issues in Chapter 7 (Random Numbers).
91
92       For  a  more  theoretical  discussion  which also covers many practical
93       issues in depth, see Chapter 3 (Random Numbers) in  Donald  E.  Knuth's
94       The  Art  of Computer Programming, volume 2 (Seminumerical Algorithms),
95       2nd ed.; Reading,  Massachusetts:  Addison-Wesley  Publishing  Company,
96       1981.
97

CONFORMING TO

99       The  functions  rand()  and  srand() conform to SVr4, 4.3BSD, C89, C99,
100       POSIX.1-2001.  The function rand_r() is from POSIX.1-2001.
101

SEE ALSO

103       drand48(3), random(3)
104
105
106
107                                  2003-11-15                           RAND(3)
Impressum