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 2009-02-03 "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)
71 _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
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)" .
84 ´Ø¿ô¤Ï¡¢ÈóÀþ·Á²ÃË¡¥Õ¥£¡¼¥É¥Ð¥Ã¥¯¤òÍѤ¤¤¿Íð¿ôÀ¸À®´Ø¿ô¤Ç¤¢¤ë¡£
85 ¤³¤Î´Ø¿ô¤Ï¡¢0 ¤«¤é \fBRAND_MAX\fR ¤Þ¤Ç¤Îµ¿»÷Íð¿ô¤òÊÖ¤¹¡£
86 ¤½¤Î¤¿¤á¤Ë 31 ¸Ä¤Î¥í¥ó¥°À°¿ô¤«¤é¤Ê¤ë¥Ç¥Õ¥©¥ë¥È¤Îɽ¤ò»ÈÍѤ¹¤ë¡£
87 ¤³¤ÎÍð¿ô¤òÀ¸À®¤¹¤ë´Ø¿ô¤Î¼þ´ü¤Ï¤È¤Æ¤âŤ¯¡¢¤ª¤è¤½
88 .I "16\ *\ ((2^31)\ \-\ 1)"
93 .\"O function sets its argument as the seed for a new
94 .\"O sequence of pseudo-random integers to be returned by
96 .\"O These sequences are repeatable by calling
100 .\"O If no seed value is provided, the
103 .\"O is automatically seeded with a value of 1.
107 ¤ÇÊÖ¤µ¤ì¤ëµ¿»÷Íð¿ôÀ°¿ô·ÏÎó¤Î¼ï¤òÀßÄꤹ¤ë¡£
108 ¤½¤Î¤¿¤á¤Ë¤Ï¿·¤·¤¤¼ï¤ò°ú¿ô¤Ë¤·¤Æ
113 °ú¿ô¤ËƱ¤¸¼ï¤ÎÃͤòÍѤ¤¤Æ
115 ¤ò¸Æ¤Ö¤³¤È¤ÇºÆ¸½²Äǽ¤Ç¤¢¤ë¡£
116 ¼ï¤ÎÃͤ¬Í¿¤¨¤é¤ì¤Ê¤¤¾ì¹ç¤Ë¤Ï
118 ´Ø¿ô¤Ï¡¢¼«Æ°Åª¤Ë 1 ¤ò¼ï¤ËÀßÄꤹ¤ë¡£
121 .\"O .BR initstate ()
122 .\"O function allows a state array \fIstate\fP to
123 .\"O be initialized for use by
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.
138 \fIstate\fP ¤ò½é´ü²½¤¹¤ë¡£
140 ¤Ç¤Ï¡¢¾õÂÖÇÛÎó¤ÎÂ礤µ \fIn\fP ¤Ï
141 »ÈÍѤ¹¤ë´Ø¿ô¤ÎÍð¿ôÀ¸À®¤ÎÀǽ¤ÎÄøÅÙ¤ò·èÄꤹ¤ë¤¿¤á¤Ë»ÈÍѤµ¤ì¤ë
142 \(em ¾õÂÖÇÛÎó¤¬Â礤¤Äø¡¢Íð¿ô¤ÎÀǽ¤Ï¤è¤¯¤Ê¤ë¡£
143 \fIseed\fP ¤Ï½é´ü²½¤Î¤¿¤á¤Î¼ï¤Ç¤¢¤ë¡£
144 ¤³¤ì¤ÏÍð¿ô·ÏÎó¤Î³«»Ï°ÌÃÖ¤ò·èÄꤹ¤ë¤â¤Î¤Ç¤¢¤ê¡¢
145 ¤³¤ÎÃͤò»ØÄꤹ¤ë¤³¤È¤ÇƱ°ì¤Î³«»Ï°ÌÃÖ¤«¤éÍð¿ô¤ÎÀ¸À®¤ò
146 ºÆ³«¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
150 .\"O function changes the state array used by the
153 .\"O The state array \fIstate\fP is used for
154 .\"O random number generation until the next call to
155 .\"O .BR initstate ()
157 .\"O .BR setstate ().
158 .\"O \fIstate\fP must first have been initialized
160 .\"O .BR initstate ()
161 .\"O or be the result of a previous call of
162 .\"O .BR setstate ().
166 ¤Ç»ÈÍѤµ¤ì¤ë¾õÂÖÇÛÎó¤òÊѹ¹¤¹¤ë¡£
167 ¾õÂÖÇÛÎó \fIstate\fP ¤Ï¡¢
172 ¼¡¤Ë¸Æ¤Ó½Ð¤µ¤ì¤ë¤Þ¤Ç¡¢Íð¿ô¤ÎÀ¸À®¤Ë»ÈÍѤµ¤ì¤ë¡£
175 ¤òÍѤ¤¤ÆºÇ½é¤Ë½é´ü²½¤µ¤ì¤Æ¤¤¤ë¤«¡¢
178 ¤Î·ë²Ì¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
179 .\"O .SH "RETURN VALUE"
183 .\"O function returns a value between 0 and
187 .\"O function returns no value.
189 .\"O .BR initstate ()
192 .\"O functions return a pointer to the previous state
193 .\"O array, or NULL on error.
204 ľÁ°¤Î¾õÂÖÇÛÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤Þ¤¿¤Ï NULL ¤òÊÖ¤¹¡£
209 .\"O A state array of less than 8 bytes was specified to
210 .\"O .BR initstate ().
212 ¤Ç8¥Ð¥¤¥È¤è¤ê¤â¾®¤µ¤¤¾õÂÖÇÛÎó¤ò»ØÄꤷ¤¿¡£
213 .\"O .SH "CONFORMING TO"
216 4.3BSD, POSIX.1-2001.
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
224 ¾õÂÖÇÛÎó \fIn\fP ¤ÎÂ礤µ¤Î¸½ºß¤Î¡ÖºÇŬ¡×ÃͤÏ
225 8¡¢32¡¢64¡¢128¡¢256 ¥Ð¥¤¥È¤Ç¤¢¤ë¡£
226 ¤½¤Î¾¤ÎÎ̤ò»ØÄꤷ¤¿¾ì¹ç¤Ë¤Ï¡¢»ØÄꤷ¤¿Î̤ò±Û¤¨¤Ê¤¤¾å½Ò¤ÎÃͤË
228 8 ¥Ð¥¤¥È̤Ëþ¤ÎÎ̤ò»ØÄꤷ¤¿¾ì¹ç¤Ë¤Ï¥¨¥é¡¼¤Î¸¶°ø¤È¤Ê¤ë¡£
230 .\"O This function should not be used in cases where multiple threads use
232 .\"O and the behavior should be reproducible.
234 .\"O .BR random_r (3)
235 .\"O for that purpose.
238 ¤ò»È¤¦¤è¤¦¤Ê¾õ¶·¤Ç¤Ï¡¢¤³¤Î´Ø¿ô¤ò»ÈÍѤ¹¤Ù¤¤Ç¤Ï¤Ê¤¤¡£
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 ¾Ï (Íð¿ô) ¤ÇŸ³«¤µ¤ì¤Æ¤¤¤ë¡£
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 ¤¿¤¯¤µ¤ó¤Î¼ÂÍÑŪ¤ÊÏÃÂê¤Ë¤Ä¤¤¤Æ¤â¿¼¤¯ÌÖÍ夵¤ì¤Æ¤¤¤ë¡£