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
139 in the Linux C Library use the same random number generator as
143 so the lower-order bits should be as random as the higher-order bits.
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.
167 SVr4, 4.3BSD, C89, POSIX.1-2001.
171 Obsolete in POSIX.1-2008.
173 POSIX.1-2001 gives the following example of an implementation of
177 possibly useful when one needs the same sequence on two different machines.
181 static unsigned long next = 1;
183 /* RAND_MAX assumed to be 32767 */
185 next = next * 1103515245 + 12345;
186 return((unsigned)(next/65536) % 32768);
189 void mysrand(unsigned int seed) {
195 The following program can be used to display the
196 pseudo-random sequence produced by
198 when given a particular seed.
201 the program uses a random seed.
204 .\" SRC BEGIN (rand.c)
210 main(int argc, char *argv[])
213 unsigned int seed, nloops;
216 fprintf(stderr, "Usage: %s <seed> <nloops>\en", argv[0]);
220 seed = atoi(argv[1]);
221 nloops = atoi(argv[2]);
225 printf("seed: %u\en", seed);
229 for (unsigned int j = 0; j < nloops; j++) {