1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\" Modified 1993-03-29, David Metcalfe
29 .\" Modified 1993-04-28, Lars Wirzenius
30 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
31 .\" Modified 1995-05-18, Rik Faith (faith@cs.unc.edu) to add
32 .\" better discussion of problems with rand on other systems.
33 .\" (Thanks to Esa Hyyti{ (ehyytia@snakemail.hut.fi).)
34 .\" Modified 1998-04-10, Nicolás Lichtmaier <nick@debian.org>
35 .\" with contribution from Francesco Potorti <F.Potorti@cnuce.cnr.it>
36 .\" Modified 2003-11-15, aeb, added rand_r
37 .\" 2010-09-13, mtk, added example program
39 .TH RAND 3 2010-10-01 "" "Linux Programmer's Manual"
41 rand, rand_r, srand \- pseudo-random number generator
44 .B #include <stdlib.h>
48 .BI "int rand_r(unsigned int *" seedp );
50 .BI "void srand(unsigned int " seed );
54 Feature Test Macro Requirements for glibc (see
55 .BR feature_test_macros (7)):
59 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
63 function returns a pseudo-random integer in the range 0 to
65 inclusive (i.e., the mathematical range [0,\ \fBRAND_MAX\fR]).
69 function sets its argument as the seed for a new
70 sequence of pseudo-random integers to be returned by
72 These sequences are repeatable by calling
74 with the same seed value.
76 If no seed value is provided, the
78 function is automatically seeded with a value of 1.
82 is not reentrant or thread-safe, since it
83 uses hidden state that is modified on each call.
84 This might just be the seed value to be used by the next call,
85 or it might be something more elaborate.
86 In order to get reproducible behavior in a threaded
87 application, this state must be made explicit;
88 this can be done using the reentrant function
94 returns a pseudo-random integer in the range [0,\ \fBRAND_MAX\fR].
97 argument is a pointer to an
99 that is used to store state between calls.
102 is called with the same initial value for the integer pointed to by
104 and that value is not modified between calls,
105 then the same pseudo-random sequence will result.
107 The value pointed to by the
111 provides only a very small amount of state,
112 so this function will be a weak pseudo-random generator.
121 functions return a value between 0 and
126 function returns no value.
132 conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
135 is from POSIX.1-2001.
144 in the Linux C Library use the same random number generator as
148 so the lower-order bits should be as random as the higher-order bits.
151 implementations, and on current implementations on different systems,
152 the lower-order bits are much less random than the higher-order bits.
153 Do not use this function in applications intended to be portable
154 when good randomness is needed.
159 POSIX.1-2001 gives the following example of an implementation of
163 possibly useful when one needs the same sequence on two different machines.
167 static unsigned long next = 1;
169 /* RAND_MAX assumed to be 32767 */
171 next = next * 1103515245 + 12345;
172 return((unsigned)(next/65536) % 32768);
175 void mysrand(unsigned seed) {
181 The following program can be used to display the
182 pseudo-random sequence produced by
184 when given a particular seed.
192 main(int argc, char *argv[])
198 fprintf(stderr, "Usage: %s <seed> <nloops>\\n", argv[0]);
202 seed = atoi(argv[1]);
203 nloops = atoi(argv[2]);
206 for (j = 0; j < nloops; j++) {