1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
30 .\" Modified 1993-03-29, David Metcalfe
31 .\" Modified 1993-04-28, Lars Wirzenius
32 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
33 .\" Modified 1995-05-18, Rik Faith (faith@cs.unc.edu) to add
34 .\" better discussion of problems with rand on other systems.
35 .\" (Thanks to Esa Hyyti{ (ehyytia@snakemail.hut.fi).)
36 .\" Modified 1998-04-10, Nicolás Lichtmaier <nick@debian.org>
37 .\" with contribution from Francesco Potorti <F.Potorti@cnuce.cnr.it>
38 .\" Modified 2003-11-15, aeb, added rand_r
39 .\" 2010-09-13, mtk, added example program
41 .TH RAND 3 2016-03-15 "" "Linux Programmer's Manual"
43 rand, rand_r, srand \- pseudo-random number generator
46 .B #include <stdlib.h>
50 .BI "int rand_r(unsigned int *" seedp );
52 .BI "void srand(unsigned int " seed );
56 Feature Test Macro Requirements for glibc (see
57 .BR feature_test_macros (7)):
65 function returns a pseudo-random integer in the range 0 to
67 inclusive (i.e., the mathematical range [0,\ \fBRAND_MAX\fR]).
71 function sets its argument as the seed for a new
72 sequence of pseudo-random integers to be returned by
74 These sequences are repeatable by calling
76 with the same seed value.
78 If no seed value is provided, the
80 function is automatically seeded with a value of 1.
84 is not reentrant, since it
85 uses hidden state that is modified on each call.
86 This might just be the seed value to be used by the next call,
87 or it might be something more elaborate.
88 In order to get reproducible behavior in a threaded
89 application, this state must be made explicit;
90 this can be done using the reentrant function
96 returns a pseudo-random integer in the range [0,\ \fBRAND_MAX\fR].
99 argument is a pointer to an
101 that is used to store state between calls.
104 is called with the same initial value for the integer pointed to by
106 and that value is not modified between calls,
107 then the same pseudo-random sequence will result.
109 The value pointed to by the
113 provides only a very small amount of state,
114 so this function will be a weak pseudo-random generator.
123 functions return a value between 0 and
128 function returns no value.
130 For an explanation of the terms used in this section, see
136 Interface Attribute Value
141 T} Thread safety MT-Safe
148 conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
151 is from POSIX.1-2001.
160 in the Linux C Library use the same random number generator as
164 so the lower-order bits should be as random as the higher-order bits.
167 implementations, and on current implementations on different systems,
168 the lower-order bits are much less random than the higher-order bits.
169 Do not use this function in applications intended to be portable
170 when good randomness is needed.
175 POSIX.1-2001 gives the following example of an implementation of
179 possibly useful when one needs the same sequence on two different machines.
183 static unsigned long next = 1;
185 /* RAND_MAX assumed to be 32767 */
187 next = next * 1103515245 + 12345;
188 return((unsigned)(next/65536) % 32768);
191 void mysrand(unsigned int seed) {
197 The following program can be used to display the
198 pseudo-random sequence produced by
200 when given a particular seed.
208 main(int argc, char *argv[])
214 fprintf(stderr, "Usage: %s <seed> <nloops>\\n", argv[0]);
218 seed = atoi(argv[1]);
219 nloops = atoi(argv[2]);
222 for (j = 0; j < nloops; j++) {