OSDN Git Service

(split) Apply minor changes from v3.32 to v3.35 in the upstream.
[linuxjm/LDP_man-pages.git] / draft / man3 / random.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 .\" Modified Sun Mar 28 00:25:51 1993, David Metcalfe
28 .\" Modified Sat Jul 24 18:13:39 1993 by Rik Faith (faith@cs.unc.edu)
29 .\" Modified Sun Aug 20 21:47:07 2000, aeb
30 .\"
31 .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi
32 .\"       all rights reserved.
33 .\" Translated 1997-01-21, YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
34 .\" Updated 2008-08-08, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
35 .\"
36 .\" WORD: nonlinear additive feedback   ÈóÀþ·Á²ÃË¡¥Õ¥£¡¼¥É¥Ð¥Ã¥¯
37 .\" WORD: state array   ¾õÂÖÇÛÎó
38 .\"
39 .TH RANDOM 3  2010-09-20 "GNU" "Linux Programmer's Manual"
40 .\"O .SH NAME
41 .SH Ì¾Á°
42 .\"O random, srandom, initstate, setstate \- random number generator
43 random, srandom, initstate, setstate \- Íð¿ô¤òÀ¸À®¤¹¤ë´Ø¿ô
44 .\"O .SH SYNOPSIS
45 .SH ½ñ¼°
46 .nf
47 .B #include <stdlib.h>
48 .sp
49 .B long int random(void);
50 .br
51 .BI "void srandom(unsigned int " seed );
52
53 .BI "char *initstate(unsigned int " seed ", char *" state ", size_t " n );
54
55 .BI "char *setstate(char *" state );
56 .fi
57 .sp
58 .in -4n
59 .\"O Feature Test Macro Requirements for glibc (see
60 .\"O .BR feature_test_macros (7)):
61 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
62 .RB ( feature_test_macros (7)
63 »²¾È):
64 .in
65 .sp
66 .ad l
67 .BR random (),
68 .BR srandom (),
69 .BR initstate (),
70 .BR setstate ():
71 .RS 4
72 _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
73 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
74 .RE
75 .ad
76 .\"O .SH DESCRIPTION
77 .SH ÀâÌÀ
78 .\"O The
79 .\"O .BR random ()
80 .\"O function uses a nonlinear additive feedback random
81 .\"O number generator employing a default table of size 31 long integers to
82 .\"O return successive pseudo-random numbers in
83 .\"O the range from 0 to \fBRAND_MAX\fR.
84 .\"O The period of this random number generator is very large, approximately
85 .\"O .IR "16\ *\ ((2^31)\ \-\ 1)" .
86 .BR random ()
87 ´Ø¿ô¤Ï¡¢ÈóÀþ·Á²ÃË¡¥Õ¥£¡¼¥É¥Ð¥Ã¥¯¤òÍѤ¤¤¿Íð¿ôÀ¸À®´Ø¿ô¤Ç¤¢¤ë¡£
88 ¤³¤Î´Ø¿ô¤Ï¡¢0 ¤«¤é \fBRAND_MAX\fR ¤Þ¤Ç¤Îµ¿»÷Íð¿ô¤òÊÖ¤¹¡£
89 ¤½¤Î¤¿¤á¤Ë 31 ¸Ä¤Î¥í¥ó¥°À°¿ô¤«¤é¤Ê¤ë¥Ç¥Õ¥©¥ë¥È¤Îɽ¤ò»ÈÍѤ¹¤ë¡£
90 ¤³¤ÎÍð¿ô¤òÀ¸À®¤¹¤ë´Ø¿ô¤Î¼þ´ü¤Ï¤È¤Æ¤âŤ¯¡¢¤ª¤è¤½
91 .I "16\ *\ ((2^31)\ \-\ 1)"
92 ¤Ç¤¢¤ë¡£
93 .PP
94 .\"O The
95 .\"O .BR srandom ()
96 .\"O function sets its argument as the seed for a new
97 .\"O sequence of pseudo-random integers to be returned by
98 .\"O .BR random ().
99 .\"O These sequences are repeatable by calling
100 .\"O .BR srandom ()
101 .\"O with the same
102 .\"O seed value.
103 .\"O If no seed value is provided, the
104 .\"O .BR random ()
105 .\"O function
106 .\"O is automatically seeded with a value of 1.
107 .BR srandom ()
108 ´Ø¿ô¤Ï¡¢
109 .BR random ()
110 ¤ÇÊÖ¤µ¤ì¤ëµ¿»÷Íð¿ôÀ°¿ô·ÏÎó¤Î¼ï¤òÀßÄꤹ¤ë¡£
111 ¤½¤Î¤¿¤á¤Ë¤Ï¿·¤·¤¤¼ï¤ò°ú¿ô¤Ë¤·¤Æ
112 .BR srandom ()
113 ¤ò¸Æ¤Ù¤Ð¤è¤¤¡£
114 .BR random ()
115 ¤ÇÀ¸À®¤µ¤ì¤ë·ÏÎó¤Ï¡¢
116 °ú¿ô¤ËƱ¤¸¼ï¤ÎÃͤòÍѤ¤¤Æ
117 .BR srandom ()
118 ¤ò¸Æ¤Ö¤³¤È¤ÇºÆ¸½²Äǽ¤Ç¤¢¤ë¡£
119 ¼ï¤ÎÃͤ¬Í¿¤¨¤é¤ì¤Ê¤¤¾ì¹ç¤Ë¤Ï
120 .BR random ()
121 ´Ø¿ô¤Ï¡¢¼«Æ°Åª¤Ë 1 ¤ò¼ï¤ËÀßÄꤹ¤ë¡£
122 .PP
123 .\"O The
124 .\"O .BR initstate ()
125 .\"O function allows a state array \fIstate\fP to
126 .\"O be initialized for use by
127 .\"O .BR random ().
128 .\"O The size of the state array
129 .\"O \fIn\fP is used by
130 .\"O .BR initstate ()
131 .\"O to decide how sophisticated a
132 .\"O random number generator it should use\(emthe larger the state array,
133 .\"O the better the random numbers will be.
134 .\"O \fIseed\fP is the seed for the
135 .\"O initialization, which specifies a starting point for the random number
136 .\"O sequence, and provides for restarting at the same point.
137 .BR initstate ()
138 ´Ø¿ô¤Ï¡¢
139 .BR random ()
140 ¤Ç»ÈÍѤµ¤ì¤ë¾õÂÖÇÛÎó
141 \fIstate\fP ¤ò½é´ü²½¤¹¤ë¡£
142 .BR initstate ()
143 ¤Ç¤Ï¡¢¾õÂÖÇÛÎó¤ÎÂ礭¤µ \fIn\fP ¤Ï
144 »ÈÍѤ¹¤ë´Ø¿ô¤ÎÍð¿ôÀ¸À®¤ÎÀ­Ç½¤ÎÄøÅÙ¤ò·èÄꤹ¤ë¤¿¤á¤Ë»ÈÍѤµ¤ì¤ë
145 \(em ¾õÂÖÇÛÎó¤¬Â礭¤¤Äø¡¢Íð¿ô¤ÎÀ­Ç½¤Ï¤è¤¯¤Ê¤ë¡£
146 \fIseed\fP ¤Ï½é´ü²½¤Î¤¿¤á¤Î¼ï¤Ç¤¢¤ë¡£
147 ¤³¤ì¤ÏÍð¿ô·ÏÎó¤Î³«»Ï°ÌÃÖ¤ò·èÄꤹ¤ë¤â¤Î¤Ç¤¢¤ê¡¢
148 ¤³¤ÎÃͤò»ØÄꤹ¤ë¤³¤È¤ÇƱ°ì¤Î³«»Ï°ÌÃÖ¤«¤éÍð¿ô¤ÎÀ¸À®¤ò
149 ºÆ³«¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
150 .PP
151 .\"O The
152 .\"O .BR setstate ()
153 .\"O function changes the state array used by the
154 .\"O .BR random ()
155 .\"O function.
156 .\"O The state array \fIstate\fP is used for
157 .\"O random number generation until the next call to
158 .\"O .BR initstate ()
159 .\"O or
160 .\"O .BR setstate ().
161 .\"O \fIstate\fP must first have been initialized
162 .\"O using
163 .\"O .BR initstate ()
164 .\"O or be the result of a previous call of
165 .\"O .BR setstate ().
166 .BR setstate ()
167 ´Ø¿ô¤Ï¡¢
168 .BR random ()
169 ¤Ç»ÈÍѤµ¤ì¤ë¾õÂÖÇÛÎó¤òÊѹ¹¤¹¤ë¡£
170 ¾õÂÖÇÛÎó \fIstate\fP ¤Ï¡¢
171 .BR initstate ()
172 ¤Þ¤¿¤Ï
173 .BR setstate ()
174 ¤¬
175 ¼¡¤Ë¸Æ¤Ó½Ð¤µ¤ì¤ë¤Þ¤Ç¡¢Íð¿ô¤ÎÀ¸À®¤Ë»ÈÍѤµ¤ì¤ë¡£
176 \fIstate\fP ¤Ï
177 .BR initstate ()
178 ¤òÍѤ¤¤ÆºÇ½é¤Ë½é´ü²½¤µ¤ì¤Æ¤¤¤ë¤«¡¢
179 °ÊÁ°¤Ë¸Æ¤Ó½Ð¤·¤¿
180 .BR setstate ()
181 ¤Î·ë²Ì¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
182 .\"O .SH "RETURN VALUE"
183 .SH ÊÖ¤êÃÍ
184 .\"O The
185 .\"O .BR random ()
186 .\"O function returns a value between 0 and
187 .\"O .BR RAND_MAX .
188 .\"O The
189 .\"O .BR srandom ()
190 .\"O function returns no value.
191 .\"O The
192 .\"O .BR initstate ()
193 .\"O function returns a pointer to the previous state array.
194 .\"O The
195 .\"O .BR setstate ()
196 .\"O function returns a pointer to the previous state array, or NULL on error.
197 .BR random ()
198 ´Ø¿ô¤Ï 0 ¤È
199 .B RAND_MAX
200 ¤Î´Ö¤ÎÃͤòÊÖ¤¹¡£
201 .BR srandom ()
202 ´Ø¿ô¤ÏÃͤòÊÖ¤µ¤Ê¤¤¡£
203 .BR initstate ()
204 ´Ø¿ô¤ÏľÁ°¤Î¾õÂÖÇÛÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
205 .BR setstate ()
206 ´Ø¿ô¤ÏľÁ°¤Î¾õÂÖÇÛÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤·¡¢
207 ¥¨¥é¡¼¤Î¾ì¹ç¤Ï NULL ¤òÊÖ¤¹¡£
208 .\"O .SH ERRORS
209 .SH ¥¨¥é¡¼
210 .TP
211 .B EINVAL
212 .\"O A state array of less than 8 bytes was specified to
213 .\"O .BR initstate ().
214 .BR initstate ()
215 ¤Ç8¥Ð¥¤¥È¤è¤ê¤â¾®¤µ¤¤¾õÂÖÇÛÎó¤ò»ØÄꤷ¤¿¡£
216 .\"O .SH "CONFORMING TO"
217 .\"O 4.3BSD
218 .SH ½àµò
219 4.3BSD, POSIX.1-2001.
220 .\"O .SH NOTES
221 .SH Ãí°Õ
222 .\"O Current "optimal" values for the size of the state array \fIn\fP are
223 .\"O 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
224 .\"O the nearest known amount.
225 .\"O Using less than 8 bytes will cause an
226 .\"O error.
227 ¾õÂÖÇÛÎó \fIn\fP ¤ÎÂ礭¤µ¤Î¸½ºß¤Î¡ÖºÇŬ¡×ÃͤÏ
228 8¡¢32¡¢64¡¢128¡¢256 ¥Ð¥¤¥È¤Ç¤¢¤ë¡£
229 ¤½¤Î¾¤ÎÎ̤ò»ØÄꤷ¤¿¾ì¹ç¤Ë¤Ï¡¢»ØÄꤷ¤¿Î̤ò±Û¤¨¤Ê¤¤¾å½Ò¤ÎÃͤË
230 ºÇ¤â¶á¤¤Ãͤˤʤ롣
231 8 ¥Ð¥¤¥È̤Ëþ¤ÎÎ̤ò»ØÄꤷ¤¿¾ì¹ç¤Ë¤Ï¥¨¥é¡¼¤Î¸¶°ø¤È¤Ê¤ë¡£
232
233 .\"O This function should not be used in cases where multiple threads use
234 .\"O .BR random ()
235 .\"O and the behavior should be reproducible.
236 .\"O Use
237 .\"O .BR random_r (3)
238 .\"O for that purpose.
239 Ê£¿ô¤Î¥¹¥ì¥Ã¥É¤¬
240 .BR random ()
241 ¤ò»È¤¦¤è¤¦¤Ê¾õ¶·¤Ç¤Ï¡¢¤³¤Î´Ø¿ô¤ò»ÈÍѤ¹¤Ù¤­¤Ç¤Ï¤Ê¤¤¡£
242 ¤½¤Î¾ì¹ç¤Ë¤Ï
243 .BR random_r (3)
244 ¤ò»È¤¦¤³¤È¡£
245
246 .\"O Random-number generation is a complex topic.
247 .\"O .I Numerical Recipes in C: The Art of Scientific Computing
248 .\"O (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William
249 .\"O T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.)
250 .\"O provides an excellent discussion of practical random-number generation
251 .\"O issues in Chapter 7 (Random Numbers).
252 Íð¿ô¤ÎÀ¸À®¤ÏÊ£»¨¤ÊÏÃÂê¤Ç¤¢¤ë¡£
253 .I Numerical Recipes in C: The Art of Scientific Computing
254 (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William
255 T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.)
256 ¤Ç¤Ï¼ÂÍÑŪ¤ÊÍð¿ôÀ¸À®¤òÏÀÅÀ¤È¤·¤¿Í¥¤ì¤¿µÄÏÀ¤¬Âè 7 ¾Ï (Íð¿ô) ¤ÇŸ³«¤µ¤ì¤Æ¤¤¤ë¡£
257
258 .\"O For a more theoretical discussion which also covers many practical issues
259 .\"O in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's
260 .\"O .IR "The Art of Computer Programming" ,
261 .\"O volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
262 .\"O Addison-Wesley Publishing Company, 1981.
263 ¤è¤êÍýÏÀŪ¤ÊµÄÏÀ¤Ë¤Ä¤¤¤Æ¤Ï Donald E. Knuth ¤Î
264 .IR "The Art of Computer Programming" ,
265 volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
266 Addison-Wesley Publishing Company, 1981
267 ¤ÎÂè 3 ¾Ï (Íð¿ô) ¤ò¸«¤è¡£¤³¤³¤Ç¤Ï¡¢
268 ¤¿¤¯¤µ¤ó¤Î¼ÂÍÑŪ¤ÊÏÃÂê¤Ë¤Ä¤¤¤Æ¤â¿¼¤¯ÌÖÍ夵¤ì¤Æ¤¤¤ë¡£
269 .\"O .SH "SEE ALSO"
270 .SH ´ØÏ¢¹àÌÜ
271 .BR drand48 (3),
272 .BR rand (3),
273 .BR random_r (3),
274 .BR srand (3)