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       int rand_r(unsigned int *seedp);
13       void srand(unsigned int seed);
14
15   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17       rand_r():
18           Since glibc 2.24:
19               _POSIX_C_SOURCE >= 199506L
20           Glibc 2.23 and earlier
21               _POSIX_C_SOURCE
22

DESCRIPTION

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

RETURN VALUE

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

ATTRIBUTES

56       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
57       tributes(7).
58
59       ┌────────────────────────────────────────────┬───────────────┬─────────┐
60Interface                                   Attribute     Value   
61       ├────────────────────────────────────────────┼───────────────┼─────────┤
62rand(), rand_r(), srand()                   │ Thread safety │ MT-Safe │
63       └────────────────────────────────────────────┴───────────────┴─────────┘
64

CONFORMING TO

66       The functions rand() and srand() conform to  SVr4,  4.3BSD,  C89,  C99,
67       POSIX.1-2001.     The   function   rand_r()   is   from   POSIX.1-2001.
68       POSIX.1-2008 marks rand_r() as obsolete.
69

NOTES

71       The versions of rand() and srand() in the Linux C Library use the  same
72       random number generator as random(3) and srandom(3), so the lower-order
73       bits should be as random as the higher-order bits.  However,  on  older
74       rand()  implementations,  and  on  current implementations on different
75       systems, the lower-order bits are much less random than the  higher-or‐
76       der bits.  Do not use this function in applications intended to be por‐
77       table when good randomness is needed.  (Use random(3) instead.)
78

EXAMPLES

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

SEE ALSO

126       drand48(3), random(3)
127

COLOPHON

129       This  page  is  part of release 5.12 of the Linux man-pages project.  A
130       description of the project, information about reporting bugs,  and  the
131       latest     version     of     this    page,    can    be    found    at
132       https://www.kernel.org/doc/man-pages/.
133
134
135
136                                  2021-03-22                           RAND(3)
Impressum