mount_setattr.2: Update supported file-systems
[man-pages.git] / man3 / rand.3
blob9f63ee9a17faaa3ea21cf35e1f371e7e57cf0805
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>
32 .B int rand(void);
33 .BI "void srand(unsigned int " seed );
35 .BI "[[deprecated]] int rand_r(unsigned int *" seedp );
36 .fi
38 .RS -4
39 Feature Test Macro Requirements for glibc (see
40 .BR feature_test_macros (7)):
41 .RE
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]).
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.
66 If no seed value is provided, the
67 .BR rand ()
68 function is automatically seeded with a value of 1.
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 ().
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.
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.
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.
104 .BR drand48_r (3)
105 instead.
106 .SH RETURN VALUE
108 .BR rand ()
110 .BR rand_r ()
111 functions return a value between 0 and
112 .B RAND_MAX
113 (inclusive).
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).
121 allbox;
122 lbx lb lb
123 l l l.
124 Interface       Attribute       Value
128 .BR rand (),
129 .BR rand_r (),
130 .BR srand ()
131 T}      Thread safety   MT-Safe
133 .SH VERSIONS
134 The versions of
135 .BR rand ()
137 .BR srand ()
138 in the Linux C Library use the same random number generator as
139 .BR random (3)
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
154 .BR rand ()
156 .BR srand ()
157 C11, POSIX.1-2008.
159 .BR rand_r ()
160 POSIX.1-2008.
161 .SH HISTORY
163 .BR rand ()
165 .BR srand ()
166 SVr4, 4.3BSD, C89, POSIX.1-2001.
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 ()
175 .BR srand (),
176 possibly useful when one needs the same sequence on two different machines.
178 .in +4n
180 static unsigned long next = 1;
182 /* RAND_MAX assumed to be 32767 */
183 int myrand(void) {
184     next = next * 1103515245 + 12345;
185     return((unsigned)(next/65536) % 32768);
188 void mysrand(unsigned int seed) {
189     next = seed;
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.
202 .in +4n
203 .\" SRC BEGIN (rand.c)
205 #include <stdio.h>
206 #include <stdlib.h>
209 main(int argc, char *argv[])
211     int           r;
212     unsigned int  seed, nloops;
214     if (argc != 3) {
215         fprintf(stderr, "Usage: %s <seed> <nloops>\en", argv[0]);
216         exit(EXIT_FAILURE);
217     }
219     seed = atoi(argv[1]);
220     nloops = atoi(argv[2]);
222     if (seed == \-1) {
223         seed = arc4random();
224         printf("seed: %u\en", seed);
225     }
227     srand(seed);
228     for (unsigned int j = 0; j < nloops; j++) {
229         r =  rand();
230         printf("%d\en", r);
231     }
233     exit(EXIT_SUCCESS);
236 .\" SRC END
238 .SH SEE ALSO
239 .BR drand48 (3),
240 .BR random (3)