]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/rand.3
getent.1, intro.1, time.1, _exit.2, _syscall.2, accept.2, access.2, acct.2, adjtimex...
[thirdparty/man-pages.git] / man3 / rand.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
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.
6 .\"
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.
11 .\"
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
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\" 386BSD man pages
27 .\"
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
38 .\"
39 .TH RAND 3 2010-10-01 "" "Linux Programmer's Manual"
40 .SH NAME
41 rand, rand_r, srand \- pseudo-random number generator
42 .SH SYNOPSIS
43 .nf
44 .B #include <stdlib.h>
45 .sp
46 .B int rand(void);
47 .sp
48 .BI "int rand_r(unsigned int *" seedp );
49 .sp
50 .BI "void srand(unsigned int " seed );
51 .fi
52 .sp
53 .in -4n
54 Feature Test Macro Requirements for glibc (see
55 .BR feature_test_macros (7)):
56 .in
57 .sp
58 .BR rand_r ():
59 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
60 .SH DESCRIPTION
61 The
62 .BR rand ()
63 function returns a pseudo-random integer in the range 0 to
64 .BR RAND_MAX
65 inclusive (i.e., the mathematical range [0,\ \fBRAND_MAX\fR]).
66 .PP
67 The
68 .BR srand ()
69 function sets its argument as the seed for a new
70 sequence of pseudo-random integers to be returned by
71 .BR rand ().
72 These sequences are repeatable by calling
73 .BR srand ()
74 with the same seed value.
75 .PP
76 If no seed value is provided, the
77 .BR rand ()
78 function is automatically seeded with a value of 1.
79 .PP
80 The function
81 .BR rand ()
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
89 .BR rand_r ().
90
91 Like
92 .BR rand (),
93 .BR rand_r ()
94 returns a pseudo-random integer in the range [0,\ \fBRAND_MAX\fR].
95 The
96 .I seedp
97 argument is a pointer to an
98 .IR "unsigned int"
99 that is used to store state between calls.
100 If
101 .BR rand_r ()
102 is called with the same initial value for the integer pointed to by
103 .IR seedp ,
104 and that value is not modified between calls,
105 then the same pseudo-random sequence will result.
106
107 The value pointed to by the
108 .I seedp
109 argument of
110 .BR rand_r ()
111 provides only a very small amount of state,
112 so this function will be a weak pseudo-random generator.
113 Try
114 .BR drand48_r (3)
115 instead.
116 .SH RETURN VALUE
117 The
118 .BR rand ()
119 and
120 .BR rand_r ()
121 functions return a value between 0 and
122 .BR RAND_MAX
123 (inclusive).
124 The
125 .BR srand ()
126 function returns no value.
127 .SH CONFORMING TO
128 The functions
129 .BR rand ()
130 and
131 .BR srand ()
132 conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
133 The function
134 .BR rand_r ()
135 is from POSIX.1-2001.
136 POSIX.1-2008 marks
137 .BR rand_r ()
138 as obsolete.
139 .SH NOTES
140 The versions of
141 .BR rand ()
142 and
143 .BR srand ()
144 in the Linux C Library use the same random number generator as
145 .BR random (3)
146 and
147 .BR srandom (3),
148 so the lower-order bits should be as random as the higher-order bits.
149 However, on older
150 .BR rand ()
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.
155 (Use
156 .BR random (3)
157 instead.)
158 .SH EXAMPLE
159 POSIX.1-2001 gives the following example of an implementation of
160 .BR rand ()
161 and
162 .BR srand (),
163 possibly useful when one needs the same sequence on two different machines.
164 .sp
165 .in +4n
166 .nf
167 static unsigned long next = 1;
168
169 /* RAND_MAX assumed to be 32767 */
170 int myrand(void) {
171 next = next * 1103515245 + 12345;
172 return((unsigned)(next/65536) % 32768);
173 }
174
175 void mysrand(unsigned seed) {
176 next = seed;
177 }
178 .fi
179 .in
180 .PP
181 The following program can be used to display the
182 pseudo-random sequence produced by
183 .BR rand ()
184 when given a particular seed.
185 .in +4n
186 .nf
187
188 #include <stdlib.h>
189 #include <stdio.h>
190
191 int
192 main(int argc, char *argv[])
193 {
194 int j, r, nloops;
195 unsigned int seed;
196
197 if (argc != 3) {
198 fprintf(stderr, "Usage: %s <seed> <nloops>\\n", argv[0]);
199 exit(EXIT_FAILURE);
200 }
201
202 seed = atoi(argv[1]);
203 nloops = atoi(argv[2]);
204
205 srand(seed);
206 for (j = 0; j < nloops; j++) {
207 r = rand();
208 printf("%d\\n", r);
209 }
210
211 exit(EXIT_SUCCESS);
212 }
213 .fi
214 .in
215 .SH SEE ALSO
216 .BR drand48 (3),
217 .BR random (3)