OSDN Git Service

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