1rand(3)                    Library Functions Manual                    rand(3)
2
3
4

NAME

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

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <stdlib.h>
13
14       int rand(void);
15       void srand(unsigned int seed);
16
17       [[deprecated]] int rand_r(unsigned int *seedp);
18
19   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21       rand_r():
22           Since glibc 2.24:
23               _POSIX_C_SOURCE >= 199506L
24           glibc 2.23 and earlier
25               _POSIX_C_SOURCE
26

DESCRIPTION

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

RETURN VALUE

56       The rand() and rand_r() functions return a value between 0 and RAND_MAX
57       (inclusive).  The srand() function returns no value.
58

ATTRIBUTES

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

VERSIONS

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

STANDARDS

79       rand()
80       srand()
81              C11, POSIX.1-2008.
82
83       rand_r()
84              POSIX.1-2008.
85

HISTORY

87       rand()
88       srand()
89              SVr4, 4.3BSD, C89, POSIX.1-2001.
90
91       rand_r()
92              POSIX.1-2001.  Obsolete in POSIX.1-2008.
93

EXAMPLES

95       POSIX.1-2001 gives the following example of an implementation of rand()
96       and  srand(),  possibly  useful when one needs the same sequence on two
97       different machines.
98
99           static unsigned long next = 1;
100
101           /* RAND_MAX assumed to be 32767 */
102           int myrand(void) {
103               next = next * 1103515245 + 12345;
104               return((unsigned)(next/65536) % 32768);
105           }
106
107           void mysrand(unsigned int seed) {
108               next = seed;
109           }
110
111       The following program can be used to display the pseudo-random sequence
112       produced  by rand() when given a particular seed.  When the seed is -1,
113       the program uses a random seed.
114
115           #include <stdio.h>
116           #include <stdlib.h>
117
118           int
119           main(int argc, char *argv[])
120           {
121               int           r;
122               unsigned int  seed, nloops;
123
124               if (argc != 3) {
125                   fprintf(stderr, "Usage: %s <seed> <nloops>\n", argv[0]);
126                   exit(EXIT_FAILURE);
127               }
128
129               seed = atoi(argv[1]);
130               nloops = atoi(argv[2]);
131
132               if (seed == -1) {
133                   seed = arc4random();
134                   printf("seed: %u\n", seed);
135               }
136
137               srand(seed);
138               for (unsigned int j = 0; j < nloops; j++) {
139                   r =  rand();
140                   printf("%d\n", r);
141               }
142
143               exit(EXIT_SUCCESS);
144           }
145

SEE ALSO

147       drand48(3), random(3)
148
149
150
151Linux man-pages 6.04              2023-03-30                           rand(3)
Impressum