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
17   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19       rand_r(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
20

DESCRIPTION

22       The  rand()  function returns a pseudo-random integer in the range 0 to
23       RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]).
24
25       The srand() function sets its argument as the seed for a  new  sequence
26       of  pseudo-random  integers  to be returned by rand().  These sequences
27       are repeatable by calling srand() with the same seed value.
28
29       If no seed value is provided,  the  rand()  function  is  automatically
30       seeded with a value of 1.
31
32       The function rand() is not reentrant or thread-safe, since it uses hid‐
33       den state that is modified on each call.  This might just be  the  seed
34       value to be used by the next call, or it might be something more elabo‐
35       rate.  In order to get reproducible behavior in a threaded application,
36       this  state must be made explicit; this can be done using the reentrant
37       function rand_r().
38
39       Like rand(), rand_r() returns a  pseudo-random  integer  in  the  range
40       [0, RAND_MAX].  The seedp argument is a pointer to an unsigned int that
41       is used to store state between calls.  If rand_r() is called  with  the
42       same  initial value for the integer pointed to by seedp, and that value
43       is not modified between calls, then  the  same  pseudo-random  sequence
44       will result.
45
46       The  value pointed to by the seedp argument of rand_r() provides only a
47       very small amount of state, so this function will be a weak pseudo-ran‐
48       dom generator.  Try drand48_r(3) instead.
49

RETURN VALUE

51       The rand() and rand_r() functions return a value between 0 and RAND_MAX
52       (inclusive).  The srand() function returns no value.
53

CONFORMING TO

55       The functions rand() and srand() conform to  SVr4,  4.3BSD,  C89,  C99,
56       POSIX.1-2001.     The   function   rand_r()   is   from   POSIX.1-2001.
57       POSIX.1-2008 marks rand_r() as obsolete.
58

NOTES

60       The versions of rand() and srand() in the Linux C Library use the  same
61       random number generator as random(3) and srandom(3), so the lower-order
62       bits should be as random as the higher-order bits.  However,  on  older
63       rand()  implementations,  and  on  current implementations on different
64       systems, the lower-order bits are much less  random  than  the  higher-
65       order  bits.   Do  not use this function in applications intended to be
66       portable when good randomness is needed.  (Use random(3) instead.)
67

EXAMPLE

69       POSIX.1-2001 gives the following example of an implementation of rand()
70       and  srand(),  possibly  useful when one needs the same sequence on two
71       different machines.
72
73           static unsigned long next = 1;
74
75           /* RAND_MAX assumed to be 32767 */
76           int myrand(void) {
77               next = next * 1103515245 + 12345;
78               return((unsigned)(next/65536) % 32768);
79           }
80
81           void mysrand(unsigned seed) {
82               next = seed;
83           }
84
85       The following program can be used to display the pseudo-random sequence
86       produced by rand() when given a particular seed.
87
88           #include <stdlib.h>
89           #include <stdio.h>
90
91           int
92           main(int argc, char *argv[])
93           {
94               int j, r, nloops;
95               unsigned int seed;
96
97               if (argc != 3) {
98                   fprintf(stderr, "Usage: %s <seed> <nloops>\n", argv[0]);
99                   exit(EXIT_FAILURE);
100               }
101
102               seed = atoi(argv[1]);
103               nloops = atoi(argv[2]);
104
105               srand(seed);
106               for (j = 0; j < nloops; j++) {
107                   r =  rand();
108                   printf("%d\n", r);
109               }
110
111               exit(EXIT_SUCCESS);
112           }
113

SEE ALSO

115       drand48(3), random(3)
116

COLOPHON

118       This  page  is  part of release 3.53 of the Linux man-pages project.  A
119       description of the project, and information about reporting  bugs,  can
120       be found at http://www.kernel.org/doc/man-pages/.
121
122
123
124                                  2010-10-01                           RAND(3)
Impressum