OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man3 / random.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 .\" Modified Sun Mar 28 00:25:51 1993, David Metcalfe
30 .\" Modified Sat Jul 24 18:13:39 1993 by Rik Faith (faith@cs.unc.edu)
31 .\" Modified Sun Aug 20 21:47:07 2000, aeb
32 .\"
33 .TH RANDOM 3  2015-02-01 "GNU" "Linux Programmer's Manual"
34 .SH NAME
35 random, srandom, initstate, setstate \- random number generator
36 .SH SYNOPSIS
37 .nf
38 .B #include <stdlib.h>
39 .sp
40 .B long int random(void);
41
42 .BI "void srandom(unsigned int " seed );
43
44 .BI "char *initstate(unsigned int " seed ", char *" state ", size_t " n );
45 .br
46 .BI "char *setstate(char *" state );
47 .fi
48 .sp
49 .in -4n
50 Feature Test Macro Requirements for glibc (see
51 .BR feature_test_macros (7)):
52 .in
53 .sp
54 .ad l
55 .BR random (),
56 .BR srandom (),
57 .BR initstate (),
58 .BR setstate ():
59 .RS 4
60 _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
61 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
62 .RE
63 .ad
64 .SH DESCRIPTION
65 The
66 .BR random ()
67 function uses a nonlinear additive feedback random
68 number generator employing a default table of size 31 long integers to
69 return successive pseudo-random numbers in
70 the range from 0 to \fBRAND_MAX\fR.
71 The period of this random number generator is very large, approximately
72 .IR "16\ *\ ((2^31)\ \-\ 1)" .
73 .PP
74 The
75 .BR srandom ()
76 function sets its argument as the seed for a new
77 sequence of pseudo-random integers to be returned by
78 .BR random ().
79 These sequences are repeatable by calling
80 .BR srandom ()
81 with the same
82 seed value.
83 If no seed value is provided, the
84 .BR random ()
85 function
86 is automatically seeded with a value of 1.
87 .PP
88 The
89 .BR initstate ()
90 function allows a state array \fIstate\fP to
91 be initialized for use by
92 .BR random ().
93 The size of the state array
94 \fIn\fP is used by
95 .BR initstate ()
96 to decide how sophisticated a
97 random number generator it should use\(emthe larger the state array,
98 the better the random numbers will be.
99 \fIseed\fP is the seed for the
100 initialization, which specifies a starting point for the random number
101 sequence, and provides for restarting at the same point.
102 .PP
103 The
104 .BR setstate ()
105 function changes the state array used by the
106 .BR random ()
107 function.
108 The state array \fIstate\fP is used for
109 random number generation until the next call to
110 .BR initstate ()
111 or
112 .BR setstate ().
113 \fIstate\fP must first have been initialized
114 using
115 .BR initstate ()
116 or be the result of a previous call of
117 .BR setstate ().
118 .SH RETURN VALUE
119 The
120 .BR random ()
121 function returns a value between 0 and
122 .BR RAND_MAX .
123 The
124 .BR srandom ()
125 function returns no value.
126
127 The
128 .BR initstate ()
129 function returns a pointer to the previous state array.
130 On error,
131 .I errno
132 is set to indicate the cause.
133
134 On success,
135 .BR setstate ()
136 returns a pointer to the previous state array.
137 On error, it returns NULL, with
138 .I errno
139 set to indicate the cause of the error.
140 .SH ERRORS
141 .TP
142 .B EINVAL
143 The
144 .I state
145 argument given to
146 .BR setstate ()
147 was NULL.
148 .TP
149 .B EINVAL
150 A state array of less than 8 bytes was specified to
151 .BR initstate ().
152 .SH ATTRIBUTES
153 .SS Multithreading (see pthreads(7))
154 The
155 .BR random (),
156 .BR srandom (),
157 .BR initstate (),
158 and
159 .BR setstate ()
160 functions are thread-safe.
161 .SH CONFORMING TO
162 4.3BSD, POSIX.1-2001.
163 .SH NOTES
164 Current "optimal" values for the size of the state array \fIn\fP are
165 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
166 the nearest known amount.
167 Using less than 8 bytes will cause an
168 error.
169 .PP
170 This function should not be used in cases where multiple threads use
171 .BR random ()
172 and the behavior should be reproducible.
173 Use
174 .BR random_r (3)
175 for that purpose.
176 .PP
177 Random-number generation is a complex topic.
178 .I Numerical Recipes in C: The Art of Scientific Computing
179 (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William
180 T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.)
181 provides an excellent discussion of practical random-number generation
182 issues in Chapter 7 (Random Numbers).
183 .PP
184 For a more theoretical discussion which also covers many practical issues
185 in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's
186 .IR "The Art of Computer Programming" ,
187 volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
188 Addison-Wesley Publishing Company, 1981.
189 .SH BUGS
190 According to POSIX,
191 .BR initstate ()
192 should return NULL on error.
193 In the glibc implementation,
194 .I errno
195 is (as specified) set on error, but the function does not return NULL.
196 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=15380
197 .SH SEE ALSO
198 .BR drand48 (3),
199 .BR getrandom (2),
200 .BR rand (3),
201 .BR random_r (3),
202 .BR srand (3)
203 .SH COLOPHON
204 This page is part of release 3.79 of the Linux
205 .I man-pages
206 project.
207 A description of the project,
208 information about reporting bugs,
209 and the latest version of this page,
210 can be found at
211 \%http://www.kernel.org/doc/man\-pages/.