OSDN Git Service

(split) Import translated manuals from JM CVS Repository.
[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  2009-02-03 "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 _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
72 .ad b
73 .\"O .SH DESCRIPTION
74 .SH ÀâÌÀ
75 .\"O The
76 .\"O .BR random ()
77 .\"O function uses a nonlinear additive feedback random
78 .\"O number generator employing a default table of size 31 long integers to
79 .\"O return successive pseudo-random numbers in
80 .\"O the range from 0 to \fBRAND_MAX\fR.
81 .\"O The period of this random number generator is very large, approximately
82 .\"O .IR "16\ *\ ((2^31)\ \-\ 1)" .
83 .BR random ()
84 ´Ø¿ô¤Ï¡¢ÈóÀþ·Á²ÃË¡¥Õ¥£¡¼¥É¥Ð¥Ã¥¯¤òÍѤ¤¤¿Íð¿ôÀ¸À®´Ø¿ô¤Ç¤¢¤ë¡£
85 ¤³¤Î´Ø¿ô¤Ï¡¢0 ¤«¤é \fBRAND_MAX\fR ¤Þ¤Ç¤Îµ¿»÷Íð¿ô¤òÊÖ¤¹¡£
86 ¤½¤Î¤¿¤á¤Ë 31 ¸Ä¤Î¥í¥ó¥°À°¿ô¤«¤é¤Ê¤ë¥Ç¥Õ¥©¥ë¥È¤Îɽ¤ò»ÈÍѤ¹¤ë¡£
87 ¤³¤ÎÍð¿ô¤òÀ¸À®¤¹¤ë´Ø¿ô¤Î¼þ´ü¤Ï¤È¤Æ¤âŤ¯¡¢¤ª¤è¤½
88 .I "16\ *\ ((2^31)\ \-\ 1)"
89 ¤Ç¤¢¤ë¡£
90 .PP
91 .\"O The
92 .\"O .BR srandom ()
93 .\"O function sets its argument as the seed for a new
94 .\"O sequence of pseudo-random integers to be returned by
95 .\"O .BR random ().
96 .\"O These sequences are repeatable by calling
97 .\"O .BR srandom ()
98 .\"O with the same
99 .\"O seed value.
100 .\"O If no seed value is provided, the
101 .\"O .BR random ()
102 .\"O function
103 .\"O is automatically seeded with a value of 1.
104 .BR srandom ()
105 ´Ø¿ô¤Ï¡¢
106 .BR random ()
107 ¤ÇÊÖ¤µ¤ì¤ëµ¿»÷Íð¿ôÀ°¿ô·ÏÎó¤Î¼ï¤òÀßÄꤹ¤ë¡£
108 ¤½¤Î¤¿¤á¤Ë¤Ï¿·¤·¤¤¼ï¤ò°ú¿ô¤Ë¤·¤Æ
109 .BR srandom ()
110 ¤ò¸Æ¤Ù¤Ð¤è¤¤¡£
111 .BR random ()
112 ¤ÇÀ¸À®¤µ¤ì¤ë·ÏÎó¤Ï¡¢
113 °ú¿ô¤ËƱ¤¸¼ï¤ÎÃͤòÍѤ¤¤Æ
114 .BR srandom ()
115 ¤ò¸Æ¤Ö¤³¤È¤ÇºÆ¸½²Äǽ¤Ç¤¢¤ë¡£
116 ¼ï¤ÎÃͤ¬Í¿¤¨¤é¤ì¤Ê¤¤¾ì¹ç¤Ë¤Ï
117 .BR random ()
118 ´Ø¿ô¤Ï¡¢¼«Æ°Åª¤Ë 1 ¤ò¼ï¤ËÀßÄꤹ¤ë¡£
119 .PP
120 .\"O The
121 .\"O .BR initstate ()
122 .\"O function allows a state array \fIstate\fP to
123 .\"O be initialized for use by
124 .\"O .BR random ().
125 .\"O The size of the state array
126 .\"O \fIn\fP is used by
127 .\"O .BR initstate ()
128 .\"O to decide how sophisticated a
129 .\"O random number generator it should use \(em the larger the state array,
130 .\"O the better the random numbers will be.
131 .\"O \fIseed\fP is the seed for the
132 .\"O initialization, which specifies a starting point for the random number
133 .\"O sequence, and provides for restarting at the same point.
134 .BR initstate ()
135 ´Ø¿ô¤Ï¡¢
136 .BR random ()
137 ¤Ç»ÈÍѤµ¤ì¤ë¾õÂÖÇÛÎó
138 \fIstate\fP ¤ò½é´ü²½¤¹¤ë¡£
139 .BR initstate ()
140 ¤Ç¤Ï¡¢¾õÂÖÇÛÎó¤ÎÂ礭¤µ \fIn\fP ¤Ï
141 »ÈÍѤ¹¤ë´Ø¿ô¤ÎÍð¿ôÀ¸À®¤ÎÀ­Ç½¤ÎÄøÅÙ¤ò·èÄꤹ¤ë¤¿¤á¤Ë»ÈÍѤµ¤ì¤ë
142 \(em ¾õÂÖÇÛÎó¤¬Â礭¤¤Äø¡¢Íð¿ô¤ÎÀ­Ç½¤Ï¤è¤¯¤Ê¤ë¡£
143 \fIseed\fP ¤Ï½é´ü²½¤Î¤¿¤á¤Î¼ï¤Ç¤¢¤ë¡£
144 ¤³¤ì¤ÏÍð¿ô·ÏÎó¤Î³«»Ï°ÌÃÖ¤ò·èÄꤹ¤ë¤â¤Î¤Ç¤¢¤ê¡¢
145 ¤³¤ÎÃͤò»ØÄꤹ¤ë¤³¤È¤ÇƱ°ì¤Î³«»Ï°ÌÃÖ¤«¤éÍð¿ô¤ÎÀ¸À®¤ò
146 ºÆ³«¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
147 .PP
148 .\"O The
149 .\"O .BR setstate ()
150 .\"O function changes the state array used by the
151 .\"O .BR random ()
152 .\"O function.
153 .\"O The state array \fIstate\fP is used for
154 .\"O random number generation until the next call to
155 .\"O .BR initstate ()
156 .\"O or
157 .\"O .BR setstate ().
158 .\"O \fIstate\fP must first have been initialized
159 .\"O using
160 .\"O .BR initstate ()
161 .\"O or be the result of a previous call of
162 .\"O .BR setstate ().
163 .BR setstate ()
164 ´Ø¿ô¤Ï¡¢
165 .BR random ()
166 ¤Ç»ÈÍѤµ¤ì¤ë¾õÂÖÇÛÎó¤òÊѹ¹¤¹¤ë¡£
167 ¾õÂÖÇÛÎó \fIstate\fP ¤Ï¡¢
168 .BR initstate ()
169 ¤Þ¤¿¤Ï
170 .BR setstate ()
171 ¤¬
172 ¼¡¤Ë¸Æ¤Ó½Ð¤µ¤ì¤ë¤Þ¤Ç¡¢Íð¿ô¤ÎÀ¸À®¤Ë»ÈÍѤµ¤ì¤ë¡£
173 \fIstate\fP ¤Ï
174 .BR initstate ()
175 ¤òÍѤ¤¤ÆºÇ½é¤Ë½é´ü²½¤µ¤ì¤Æ¤¤¤ë¤«¡¢
176 °ÊÁ°¤Ë¸Æ¤Ó½Ð¤·¤¿
177 .BR setstate ()
178 ¤Î·ë²Ì¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
179 .\"O .SH "RETURN VALUE"
180 .SH ÊÖ¤êÃÍ
181 .\"O The
182 .\"O .BR random ()
183 .\"O function returns a value between 0 and
184 .\"O .BR RAND_MAX .
185 .\"O The
186 .\"O .BR srandom ()
187 .\"O function returns no value.
188 .\"O The
189 .\"O .BR initstate ()
190 .\"O and
191 .\"O .BR setstate ()
192 .\"O functions return a pointer to the previous state
193 .\"O array, or NULL on error.
194 .BR random ()
195 ´Ø¿ô¤Ï 0 ¤È
196 .B RAND_MAX
197 ¤Î´Ö¤ÎÃͤòÊÖ¤¹¡£
198 .BR srandom ()
199 ´Ø¿ô¤ÏÃͤòÊÖ¤µ¤Ê¤¤¡£
200 .BR initstate ()
201 ´Ø¿ô¤È
202 .BR setstate ()
203 ´Ø¿ô¤Ï
204 ľÁ°¤Î¾õÂÖÇÛÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤Þ¤¿¤Ï NULL ¤òÊÖ¤¹¡£
205 .\"O .SH ERRORS
206 .SH ¥¨¥é¡¼
207 .TP
208 .B EINVAL
209 .\"O A state array of less than 8 bytes was specified to
210 .\"O .BR initstate ().
211 .BR initstate ()
212 ¤Ç8¥Ð¥¤¥È¤è¤ê¤â¾®¤µ¤¤¾õÂÖÇÛÎó¤ò»ØÄꤷ¤¿¡£
213 .\"O .SH "CONFORMING TO"
214 .\"O 4.3BSD
215 .SH ½àµò
216 4.3BSD, POSIX.1-2001.
217 .\"O .SH NOTES
218 .SH Ãí°Õ
219 .\"O Current "optimal" values for the size of the state array \fIn\fP are
220 .\"O 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
221 .\"O the nearest known amount.
222 .\"O Using less than 8 bytes will cause an
223 .\"O error.
224 ¾õÂÖÇÛÎó \fIn\fP ¤ÎÂ礭¤µ¤Î¸½ºß¤Î¡ÖºÇŬ¡×ÃͤÏ
225 8¡¢32¡¢64¡¢128¡¢256 ¥Ð¥¤¥È¤Ç¤¢¤ë¡£
226 ¤½¤Î¾¤ÎÎ̤ò»ØÄꤷ¤¿¾ì¹ç¤Ë¤Ï¡¢»ØÄꤷ¤¿Î̤ò±Û¤¨¤Ê¤¤¾å½Ò¤ÎÃͤË
227 ºÇ¤â¶á¤¤Ãͤˤʤ롣
228 8 ¥Ð¥¤¥È̤Ëþ¤ÎÎ̤ò»ØÄꤷ¤¿¾ì¹ç¤Ë¤Ï¥¨¥é¡¼¤Î¸¶°ø¤È¤Ê¤ë¡£
229
230 .\"O This function should not be used in cases where multiple threads use
231 .\"O .BR random ()
232 .\"O and the behavior should be reproducible.
233 .\"O Use
234 .\"O .BR random_r (3)
235 .\"O for that purpose.
236 Ê£¿ô¤Î¥¹¥ì¥Ã¥É¤¬
237 .BR random ()
238 ¤ò»È¤¦¤è¤¦¤Ê¾õ¶·¤Ç¤Ï¡¢¤³¤Î´Ø¿ô¤ò»ÈÍѤ¹¤Ù¤­¤Ç¤Ï¤Ê¤¤¡£
239 ¤½¤Î¾ì¹ç¤Ë¤Ï
240 .BR random_r (3)
241 ¤ò»È¤¦¤³¤È¡£
242
243 .\"O Random-number generation is a complex topic.
244 .\"O .I Numerical Recipes in C: The Art of Scientific Computing
245 .\"O (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William
246 .\"O T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.)
247 .\"O provides an excellent discussion of practical random-number generation
248 .\"O issues in Chapter 7 (Random Numbers).
249 Íð¿ô¤ÎÀ¸À®¤ÏÊ£»¨¤ÊÏÃÂê¤Ç¤¢¤ë¡£
250 .I Numerical Recipes in C: The Art of Scientific Computing
251 (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William
252 T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.)
253 ¤Ç¤Ï¼ÂÍÑŪ¤ÊÍð¿ôÀ¸À®¤òÏÀÅÀ¤È¤·¤¿Í¥¤ì¤¿µÄÏÀ¤¬Âè 7 ¾Ï (Íð¿ô) ¤ÇŸ³«¤µ¤ì¤Æ¤¤¤ë¡£
254
255 .\"O For a more theoretical discussion which also covers many practical issues
256 .\"O in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's
257 .\"O .IR "The Art of Computer Programming" ,
258 .\"O volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
259 .\"O Addison-Wesley Publishing Company, 1981.
260 ¤è¤êÍýÏÀŪ¤ÊµÄÏÀ¤Ë¤Ä¤¤¤Æ¤Ï Donald E. Knuth ¤Î
261 .IR "The Art of Computer Programming" ,
262 volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
263 Addison-Wesley Publishing Company, 1981
264 ¤ÎÂè 3 ¾Ï (Íð¿ô) ¤ò¸«¤è¡£¤³¤³¤Ç¤Ï¡¢
265 ¤¿¤¯¤µ¤ó¤Î¼ÂÍÑŪ¤ÊÏÃÂê¤Ë¤Ä¤¤¤Æ¤â¿¼¤¯ÌÖÍ夵¤ì¤Æ¤¤¤ë¡£
266 .\"O .SH "SEE ALSO"
267 .SH ´ØÏ¢¹àÌÜ
268 .BR drand48 (3),
269 .BR rand (3),
270 .BR random_r (3),
271 .BR srand (3)