]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/rand.3
man*/: ffix (un-bracket tables)
[thirdparty/man-pages.git] / man3 / rand.3
1 '\" t
2 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
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 (faith@cs.unc.edu)
14 .\" Modified 1995-05-18, Rik Faith (faith@cs.unc.edu) to add
15 .\" better discussion of problems with rand on other systems.
16 .\" (Thanks to Esa Hyyti{ (ehyytia@snakemail.hut.fi).)
17 .\" Modified 1998-04-10, Nicolás Lichtmaier <nick@debian.org>
18 .\" with contribution from Francesco Potorti <F.Potorti@cnuce.cnr.it>
19 .\" Modified 2003-11-15, aeb, added rand_r
20 .\" 2010-09-13, mtk, added example program
21 .\"
22 .TH rand 3 (date) "Linux man-pages (unreleased)"
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 .PP
32 .B int rand(void);
33 .BI "void srand(unsigned int " seed );
34 .PP
35 .BI "[[deprecated]] int rand_r(unsigned int *" seedp );
36 .fi
37 .PP
38 .RS -4
39 Feature Test Macro Requirements for glibc (see
40 .BR feature_test_macros (7)):
41 .RE
42 .PP
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 .PP
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 .PP
66 If no seed value is provided, the
67 .BR rand ()
68 function is automatically seeded with a value of 1.
69 .PP
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 .PP
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 .PP
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 .sp 1
134 .SH VERSIONS
135 The versions of
136 .BR rand ()
137 and
138 .BR srand ()
139 in the Linux C Library use the same random number generator as
140 .BR random (3)
141 and
142 .BR srandom (3),
143 so the lower-order bits should be as random as the higher-order bits.
144 However, on older
145 .BR rand ()
146 implementations, and on current implementations on different systems,
147 the lower-order bits are much less random than the higher-order bits.
148 Do not use this function in applications intended to be portable
149 when good randomness is needed.
150 (Use
151 .BR random (3)
152 instead.)
153 .SH STANDARDS
154 .TP
155 .BR rand ()
156 .TQ
157 .BR srand ()
158 C11, POSIX.1-2008.
159 .TP
160 .BR rand_r ()
161 POSIX.1-2008.
162 .SH HISTORY
163 .TP
164 .BR rand ()
165 .TQ
166 .BR srand ()
167 SVr4, 4.3BSD, C89, POSIX.1-2001.
168 .TP
169 .BR rand_r ()
170 POSIX.1-2001.
171 Obsolete in POSIX.1-2008.
172 .SH EXAMPLES
173 POSIX.1-2001 gives the following example of an implementation of
174 .BR rand ()
175 and
176 .BR srand (),
177 possibly useful when one needs the same sequence on two different machines.
178 .PP
179 .in +4n
180 .EX
181 static unsigned long next = 1;
182 \&
183 /* RAND_MAX assumed to be 32767 */
184 int myrand(void) {
185 next = next * 1103515245 + 12345;
186 return((unsigned)(next/65536) % 32768);
187 }
188 \&
189 void mysrand(unsigned int seed) {
190 next = seed;
191 }
192 .EE
193 .in
194 .PP
195 The following program can be used to display the
196 pseudo-random sequence produced by
197 .BR rand ()
198 when given a particular seed.
199 When the seed is
200 .IR \-1 ,
201 the program uses a random seed.
202 .PP
203 .in +4n
204 .\" SRC BEGIN (rand.c)
205 .EX
206 #include <stdio.h>
207 #include <stdlib.h>
208 \&
209 int
210 main(int argc, char *argv[])
211 {
212 int r;
213 unsigned int seed, nloops;
214 \&
215 if (argc != 3) {
216 fprintf(stderr, "Usage: %s <seed> <nloops>\en", argv[0]);
217 exit(EXIT_FAILURE);
218 }
219 \&
220 seed = atoi(argv[1]);
221 nloops = atoi(argv[2]);
222 \&
223 if (seed == \-1) {
224 seed = arc4random();
225 printf("seed: %u\en", seed);
226 }
227 \&
228 srand(seed);
229 for (unsigned int j = 0; j < nloops; j++) {
230 r = rand();
231 printf("%d\en", r);
232 }
233 \&
234 exit(EXIT_SUCCESS);
235 }
236 .EE
237 .\" SRC END
238 .in
239 .SH SEE ALSO
240 .BR drand48 (3),
241 .BR random (3)