1RAND(3) Linux Programmer's Manual RAND(3)
2
3
4
6 rand, rand_r, srand - pseudo-random number generator
7
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():
20 Since glibc 2.24:
21 _POSIX_C_SOURCE >= 199506L
22 Glibc 2.23 and earlier
23 _POSIX_C_SOURCE
24
26 The rand() function returns a pseudo-random integer in the range 0 to
27 RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]).
28
29 The srand() function sets its argument as the seed for a new sequence
30 of pseudo-random integers to be returned by rand(). These sequences
31 are repeatable by calling srand() with the same seed value.
32
33 If no seed value is provided, the rand() function is automatically
34 seeded with a value of 1.
35
36 The function rand() is not reentrant, since it uses hidden state that
37 is modified on each call. This might just be the seed value to be used
38 by the next call, or it might be something more elaborate. In order to
39 get reproducible behavior in a threaded application, this state must be
40 made explicit; this can be done using the reentrant function rand_r().
41
42 Like rand(), rand_r() returns a pseudo-random integer in the range
43 [0, RAND_MAX]. The seedp argument is a pointer to an unsigned int that
44 is used to store state between calls. If rand_r() is called with the
45 same initial value for the integer pointed to by seedp, and that value
46 is not modified between calls, then the same pseudo-random sequence
47 will result.
48
49 The value pointed to by the seedp argument of rand_r() provides only a
50 very small amount of state, so this function will be a weak pseudo-ran‐
51 dom generator. Try drand48_r(3) instead.
52
54 The rand() and rand_r() functions return a value between 0 and RAND_MAX
55 (inclusive). The srand() function returns no value.
56
58 For an explanation of the terms used in this section, see
59 attributes(7).
60
61 ┌──────────────────────────┬───────────────┬─────────┐
62 │Interface │ Attribute │ Value │
63 ├──────────────────────────┼───────────────┼─────────┤
64 │rand(), rand_r(), srand() │ Thread safety │ MT-Safe │
65 └──────────────────────────┴───────────────┴─────────┘
67 The functions rand() and srand() conform to SVr4, 4.3BSD, C89, C99,
68 POSIX.1-2001. The function rand_r() is from POSIX.1-2001.
69 POSIX.1-2008 marks rand_r() as obsolete.
70
72 The versions of rand() and srand() in the Linux C Library use the same
73 random number generator as random(3) and srandom(3), so the lower-order
74 bits should be as random as the higher-order bits. However, on older
75 rand() implementations, and on current implementations on different
76 systems, the lower-order bits are much less random than the higher-
77 order bits. Do not use this function in applications intended to be
78 portable when good randomness is needed. (Use random(3) instead.)
79
81 POSIX.1-2001 gives the following example of an implementation of rand()
82 and srand(), possibly useful when one needs the same sequence on two
83 different machines.
84
85 static unsigned long next = 1;
86
87 /* RAND_MAX assumed to be 32767 */
88 int myrand(void) {
89 next = next * 1103515245 + 12345;
90 return((unsigned)(next/65536) % 32768);
91 }
92
93 void mysrand(unsigned int seed) {
94 next = seed;
95 }
96
97 The following program can be used to display the pseudo-random sequence
98 produced by rand() when given a particular seed.
99
100 #include <stdlib.h>
101 #include <stdio.h>
102
103 int
104 main(int argc, char *argv[])
105 {
106 int j, r, nloops;
107 unsigned int seed;
108
109 if (argc != 3) {
110 fprintf(stderr, "Usage: %s <seed> <nloops>\n", argv[0]);
111 exit(EXIT_FAILURE);
112 }
113
114 seed = atoi(argv[1]);
115 nloops = atoi(argv[2]);
116
117 srand(seed);
118 for (j = 0; j < nloops; j++) {
119 r = rand();
120 printf("%d\n", r);
121 }
122
123 exit(EXIT_SUCCESS);
124 }
125
127 drand48(3), random(3)
128
130 This page is part of release 4.16 of the Linux man-pages project. A
131 description of the project, information about reporting bugs, and the
132 latest version of this page, can be found at
133 https://www.kernel.org/doc/man-pages/.
134
135
136
137 2017-07-13 RAND(3)