1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
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.
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.
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
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
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
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>
36 .\" WORD: nonlinear additive feedback ÈóÀþ·Á²ÃË¡¥Õ¥£¡¼¥É¥Ð¥Ã¥¯
37 .\" WORD: state array ¾õÂÖÇÛÎó
39 .TH RANDOM 3 2010-09-20 "GNU" "Linux Programmer's Manual"
42 .\"O random, srandom, initstate, setstate \- random number generator
43 random, srandom, initstate, setstate \- Íð¿ô¤òÀ¸À®¤¹¤ë´Ø¿ô
47 .B #include <stdlib.h>
49 .B long int random(void);
51 .BI "void srandom(unsigned int " seed );
53 .BI "char *initstate(unsigned int " seed ", char *" state ", size_t " n );
55 .BI "char *setstate(char *" state );
59 .\"O Feature Test Macro Requirements for glibc (see
60 .\"O .BR feature_test_macros (7)):
61 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
62 .RB ( feature_test_macros (7)
72 _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
73 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
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)" .
87 ´Ø¿ô¤Ï¡¢ÈóÀþ·Á²ÃË¡¥Õ¥£¡¼¥É¥Ð¥Ã¥¯¤òÍѤ¤¤¿Íð¿ôÀ¸À®´Ø¿ô¤Ç¤¢¤ë¡£
88 ¤³¤Î´Ø¿ô¤Ï¡¢0 ¤«¤é \fBRAND_MAX\fR ¤Þ¤Ç¤Îµ¿»÷Íð¿ô¤òÊÖ¤¹¡£
89 ¤½¤Î¤¿¤á¤Ë 31 ¸Ä¤Î¥í¥ó¥°À°¿ô¤«¤é¤Ê¤ë¥Ç¥Õ¥©¥ë¥È¤Îɽ¤ò»ÈÍѤ¹¤ë¡£
90 ¤³¤ÎÍð¿ô¤òÀ¸À®¤¹¤ë´Ø¿ô¤Î¼þ´ü¤Ï¤È¤Æ¤âŤ¯¡¢¤ª¤è¤½
91 .I "16\ *\ ((2^31)\ \-\ 1)"
96 .\"O function sets its argument as the seed for a new
97 .\"O sequence of pseudo-random integers to be returned by
99 .\"O These sequences are repeatable by calling
103 .\"O If no seed value is provided, the
106 .\"O is automatically seeded with a value of 1.
110 ¤ÇÊÖ¤µ¤ì¤ëµ¿»÷Íð¿ôÀ°¿ô·ÏÎó¤Î¼ï¤òÀßÄꤹ¤ë¡£
111 ¤½¤Î¤¿¤á¤Ë¤Ï¿·¤·¤¤¼ï¤ò°ú¿ô¤Ë¤·¤Æ
116 °ú¿ô¤ËƱ¤¸¼ï¤ÎÃͤòÍѤ¤¤Æ
118 ¤ò¸Æ¤Ö¤³¤È¤ÇºÆ¸½²Äǽ¤Ç¤¢¤ë¡£
119 ¼ï¤ÎÃͤ¬Í¿¤¨¤é¤ì¤Ê¤¤¾ì¹ç¤Ë¤Ï
121 ´Ø¿ô¤Ï¡¢¼«Æ°Åª¤Ë 1 ¤ò¼ï¤ËÀßÄꤹ¤ë¡£
124 .\"O .BR initstate ()
125 .\"O function allows a state array \fIstate\fP to
126 .\"O be initialized for use by
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.
141 \fIstate\fP ¤ò½é´ü²½¤¹¤ë¡£
143 ¤Ç¤Ï¡¢¾õÂÖÇÛÎó¤ÎÂ礤µ \fIn\fP ¤Ï
144 »ÈÍѤ¹¤ë´Ø¿ô¤ÎÍð¿ôÀ¸À®¤ÎÀǽ¤ÎÄøÅÙ¤ò·èÄꤹ¤ë¤¿¤á¤Ë»ÈÍѤµ¤ì¤ë
145 \(em ¾õÂÖÇÛÎó¤¬Â礤¤Äø¡¢Íð¿ô¤ÎÀǽ¤Ï¤è¤¯¤Ê¤ë¡£
146 \fIseed\fP ¤Ï½é´ü²½¤Î¤¿¤á¤Î¼ï¤Ç¤¢¤ë¡£
147 ¤³¤ì¤ÏÍð¿ô·ÏÎó¤Î³«»Ï°ÌÃÖ¤ò·èÄꤹ¤ë¤â¤Î¤Ç¤¢¤ê¡¢
148 ¤³¤ÎÃͤò»ØÄꤹ¤ë¤³¤È¤ÇƱ°ì¤Î³«»Ï°ÌÃÖ¤«¤éÍð¿ô¤ÎÀ¸À®¤ò
149 ºÆ³«¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
153 .\"O function changes the state array used by the
156 .\"O The state array \fIstate\fP is used for
157 .\"O random number generation until the next call to
158 .\"O .BR initstate ()
160 .\"O .BR setstate ().
161 .\"O \fIstate\fP must first have been initialized
163 .\"O .BR initstate ()
164 .\"O or be the result of a previous call of
165 .\"O .BR setstate ().
169 ¤Ç»ÈÍѤµ¤ì¤ë¾õÂÖÇÛÎó¤òÊѹ¹¤¹¤ë¡£
170 ¾õÂÖÇÛÎó \fIstate\fP ¤Ï¡¢
175 ¼¡¤Ë¸Æ¤Ó½Ð¤µ¤ì¤ë¤Þ¤Ç¡¢Íð¿ô¤ÎÀ¸À®¤Ë»ÈÍѤµ¤ì¤ë¡£
178 ¤òÍѤ¤¤ÆºÇ½é¤Ë½é´ü²½¤µ¤ì¤Æ¤¤¤ë¤«¡¢
181 ¤Î·ë²Ì¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
182 .\"O .SH "RETURN VALUE"
186 .\"O function returns a value between 0 and
190 .\"O function returns no value.
192 .\"O .BR initstate ()
193 .\"O function returns a pointer to the previous state array.
196 .\"O function returns a pointer to the previous state array, or NULL on error.
204 ´Ø¿ô¤ÏľÁ°¤Î¾õÂÖÇÛÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
206 ´Ø¿ô¤ÏľÁ°¤Î¾õÂÖÇÛÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤·¡¢
207 ¥¨¥é¡¼¤Î¾ì¹ç¤Ï NULL ¤òÊÖ¤¹¡£
212 .\"O A state array of less than 8 bytes was specified to
213 .\"O .BR initstate ().
215 ¤Ç8¥Ð¥¤¥È¤è¤ê¤â¾®¤µ¤¤¾õÂÖÇÛÎó¤ò»ØÄꤷ¤¿¡£
216 .\"O .SH "CONFORMING TO"
219 4.3BSD, POSIX.1-2001.
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
227 ¾õÂÖÇÛÎó \fIn\fP ¤ÎÂ礤µ¤Î¸½ºß¤Î¡ÖºÇŬ¡×ÃͤÏ
228 8¡¢32¡¢64¡¢128¡¢256 ¥Ð¥¤¥È¤Ç¤¢¤ë¡£
229 ¤½¤Î¾¤ÎÎ̤ò»ØÄꤷ¤¿¾ì¹ç¤Ë¤Ï¡¢»ØÄꤷ¤¿Î̤ò±Û¤¨¤Ê¤¤¾å½Ò¤ÎÃͤË
231 8 ¥Ð¥¤¥È̤Ëþ¤ÎÎ̤ò»ØÄꤷ¤¿¾ì¹ç¤Ë¤Ï¥¨¥é¡¼¤Î¸¶°ø¤È¤Ê¤ë¡£
233 .\"O This function should not be used in cases where multiple threads use
235 .\"O and the behavior should be reproducible.
237 .\"O .BR random_r (3)
238 .\"O for that purpose.
241 ¤ò»È¤¦¤è¤¦¤Ê¾õ¶·¤Ç¤Ï¡¢¤³¤Î´Ø¿ô¤ò»ÈÍѤ¹¤Ù¤¤Ç¤Ï¤Ê¤¤¡£
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 ¾Ï (Íð¿ô) ¤ÇŸ³«¤µ¤ì¤Æ¤¤¤ë¡£
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 ¤¿¤¯¤µ¤ó¤Î¼ÂÍÑŪ¤ÊÏÃÂê¤Ë¤Ä¤¤¤Æ¤â¿¼¤¯ÌÖÍ夵¤ì¤Æ¤¤¤ë¡£