2 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" References consulted:
7 .\" Linux libc source code
8 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
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
22 .TH rand 3 (date) "Linux man-pages (unreleased)"
24 rand, rand_r, srand \- pseudo-random number generator
27 .RI ( libc ", " \-lc )
30 .B #include <stdlib.h>
33 .BI "void srand(unsigned int " seed );
35 .BI "[[deprecated]] int rand_r(unsigned int *" seedp );
39 Feature Test Macro Requirements for glibc (see
40 .BR feature_test_macros (7)):
46 _POSIX_C_SOURCE >= 199506L
47 glibc 2.23 and earlier
53 function returns a pseudo-random integer in the range 0 to
55 inclusive (i.e., the mathematical range [0,\ \fBRAND_MAX\fR]).
59 function sets its argument as the seed for a new
60 sequence of pseudo-random integers to be returned by
62 These sequences are repeatable by calling
64 with the same seed value.
66 If no seed value is provided, the
68 function is automatically seeded with a value of 1.
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
84 returns a pseudo-random integer in the range [0,\ \fBRAND_MAX\fR].
87 argument is a pointer to an
89 that is used to store state between calls.
92 is called with the same initial value for the integer pointed to by
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
101 provides only a very small amount of state,
102 so this function will be a weak pseudo-random generator.
111 functions return a value between 0 and
116 function returns no value.
118 For an explanation of the terms used in this section, see
124 Interface Attribute Value
131 T} Thread safety MT-Safe
138 in the Linux C Library use the same random number generator as
142 so the lower-order bits should be as random as the higher-order bits.
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.
166 SVr4, 4.3BSD, C89, POSIX.1-2001.
170 Obsolete in POSIX.1-2008.
172 POSIX.1-2001 gives the following example of an implementation of
176 possibly useful when one needs the same sequence on two different machines.
180 static unsigned long next = 1;
182 /* RAND_MAX assumed to be 32767 */
184 next = next * 1103515245 + 12345;
185 return((unsigned)(next/65536) % 32768);
188 void mysrand(unsigned int seed) {
194 The following program can be used to display the
195 pseudo-random sequence produced by
197 when given a particular seed.
200 the program uses a random seed.
203 .\" SRC BEGIN (rand.c)
209 main(int argc, char *argv[])
212 unsigned int seed, nloops;
215 fprintf(stderr, "Usage: %s <seed> <nloops>\en", argv[0]);
219 seed = atoi(argv[1]);
220 nloops = atoi(argv[2]);
224 printf("seed: %u\en", seed);
228 for (unsigned int j = 0; j < nloops; j++) {