Debian Codegraph @

upstream/debian-trixie/man3/rand.3

raw
1 '\" t
2 .\" Copyright 1993 David Metcalfe ([email protected])
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .\" References consulted:
7 .\" Linux libc source code
8 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
9 .\" 386BSD man pages
10 .\"
11 .\" Modified 1993-03-29, David Metcalfe
12 .\" Modified 1993-04-28, Lars Wirzenius
13 .\" Modified 1993-07-24, Rik Faith ([email protected])
14 .\" Modified 1995-05-18, Rik Faith ([email protected]) to add
15 .\" better discussion of problems with rand on other systems.
16 .\" (Thanks to Esa Hyyti{ ([email protected]).)
17 .\" Modified 1998-04-10, Nicolás Lichtmaier <[email protected]>
18 .\" with contribution from Francesco Potorti <[email protected]>
19 .\" Modified 2003-11-15, aeb, added rand_r
20 .\" 2010-09-13, mtk, added example program
21 .\"
22 .TH rand 3 2024-06-15 "Linux man-pages 6.9.1"
23 .SH NAME
24 rand, rand_r, srand \- pseudo-random number generator
25 .SH LIBRARY
26 Standard C library
27 .RI ( libc ", " \-lc )
28 .SH SYNOPSIS
29 .nf
30 .B #include <stdlib.h>
31 .P
32 .B int rand(void);
33 .BI "void srand(unsigned int " seed );
34 .P
35 .BI "[[deprecated]] int rand_r(unsigned int *" seedp );
36 .fi
37 .P
38 .RS -4
39 Feature Test Macro Requirements for glibc (see
40 .BR feature_test_macros (7)):
41 .RE
42 .P
43 .BR rand_r ():
44 .nf
45 Since glibc 2.24:
46 _POSIX_C_SOURCE >= 199506L
47 glibc 2.23 and earlier
48 _POSIX_C_SOURCE
49 .fi
50 .SH DESCRIPTION
51 The
52 .BR rand ()
53 function returns a pseudo-random integer in the range 0 to
54 .B RAND_MAX
55 inclusive (i.e., the mathematical range [0,\ \fBRAND_MAX\fR]).
56 .P
57 The
58 .BR srand ()
59 function sets its argument as the seed for a new
60 sequence of pseudo-random integers to be returned by
61 .BR rand ().
62 These sequences are repeatable by calling
63 .BR srand ()
64 with the same seed value.
65 .P
66 If no seed value is provided, the
67 .BR rand ()
68 function is automatically seeded with a value of 1.
69 .P
70 The function
71 .BR rand ()
72 is not reentrant, since it
73 uses hidden state that is modified on each call.
74 This might just be the seed value to be used by the next call,
75 or it might be something more elaborate.
76 In order to get reproducible behavior in a threaded
77 application, this state must be made explicit;
78 this can be done using the reentrant function
79 .BR rand_r ().
80 .P
81 Like
82 .BR rand (),
83 .BR rand_r ()
84 returns a pseudo-random integer in the range [0,\ \fBRAND_MAX\fR].
85 The
86 .I seedp
87 argument is a pointer to an
88 .I unsigned int
89 that is used to store state between calls.
90 If
91 .BR rand_r ()
92 is called with the same initial value for the integer pointed to by
93 .IR seedp ,
94 and that value is not modified between calls,
95 then the same pseudo-random sequence will result.
96 .P
97 The value pointed to by the
98 .I seedp
99 argument of
100 .BR rand_r ()
101 provides only a very small amount of state,
102 so this function will be a weak pseudo-random generator.
103 Try
104 .BR drand48_r (3)
105 instead.
106 .SH RETURN VALUE
107 The
108 .BR rand ()
109 and
110 .BR rand_r ()
111 functions return a value between 0 and
112 .B RAND_MAX
113 (inclusive).
114 The
115 .BR srand ()
116 function returns no value.
117 .SH ATTRIBUTES
118 For an explanation of the terms used in this section, see
119 .BR attributes (7).
120 .TS
121 allbox;
122 lbx lb lb
123 l l l.
124 Interface Attribute Value
125 T{
126 .na
127 .nh
128 .BR rand (),
129 .BR rand_r (),
130 .BR srand ()
131 T} Thread safety MT-Safe
132 .TE
133 .SH VERSIONS
134 The versions of
135 .BR rand ()
136 and
137 .BR srand ()
138 in the Linux C Library use the same random number generator as
139 .BR random (3)
140 and
141 .BR srandom (3),
142 so the lower-order bits should be as random as the higher-order bits.
143 However, on older
144 .BR rand ()
145 implementations, and on current implementations on different systems,
146 the lower-order bits are much less random than the higher-order bits.
147 Do not use this function in applications intended to be portable
148 when good randomness is needed.
149 (Use
150 .BR random (3)
151 instead.)
152 .SH STANDARDS
153 .TP
154 .BR rand ()
155 .TQ
156 .BR srand ()
157 C11, POSIX.1-2008.
158 .TP
159 .BR rand_r ()
160 POSIX.1-2008.
161 .SH HISTORY
162 .TP
163 .BR rand ()
164 .TQ
165 .BR srand ()
166 SVr4, 4.3BSD, C89, POSIX.1-2001.
167 .TP
168 .BR rand_r ()
169 POSIX.1-2001.
170 Obsolete in POSIX.1-2008.
171 .SH EXAMPLES
172 POSIX.1-2001 gives the following example of an implementation of
173 .BR rand ()
174 and
175 .BR srand (),
176 possibly useful when one needs the same sequence on two different machines.
177 .P
178 .in +4n
179 .EX
180 static unsigned long next = 1;
181 \&
182 /* RAND_MAX assumed to be 32767 */
183 int myrand(void) {
184 next = next * 1103515245 + 12345;
185 return((unsigned)(next/65536) % 32768);
186 }
187 \&
188 void mysrand(unsigned int seed) {
189 next = seed;
190 }
191 .EE
192 .in
193 .P
194 The following program can be used to display the
195 pseudo-random sequence produced by
196 .BR rand ()
197 when given a particular seed.
198 When the seed is
199 .IR \-1 ,
200 the program uses a random seed.
201 .P
202 .in +4n
203 .\" SRC BEGIN (rand.c)
204 .EX
205 #include <stdio.h>
206 #include <stdlib.h>
207 \&
208 int
209 main(int argc, char *argv[])
210 {
211 int r;
212 unsigned int seed, nloops;
213 \&
214 if (argc != 3) {
215 fprintf(stderr, "Usage: %s <seed> <nloops>\[rs]n", argv[0]);
216 exit(EXIT_FAILURE);
217 }
218 \&
219 seed = atoi(argv[1]);
220 nloops = atoi(argv[2]);
221 \&
222 if (seed == \-1) {
223 seed = arc4random();
224 printf("seed: %u\[rs]n", seed);
225 }
226 \&
227 srand(seed);
228 for (unsigned int j = 0; j < nloops; j++) {
229 r = rand();
230 printf("%d\[rs]n", r);
231 }
232 \&
233 exit(EXIT_SUCCESS);
234 }
235 .EE
236 .\" SRC END
237 .in
238 .SH SEE ALSO
239 .BR drand48 (3),
240 .BR random (3)