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)
28 .\" Modified 1993-03-29, David Metcalfe
29 .\" Modified 1993-04-28, Lars Wirzenius
30 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
31 .\" Modified 1995-05-18, Rik Faith (faith@cs.unc.edu) to add
32 .\" better discussion of problems with rand on other systems.
33 .\" (Thanks to Esa Hyyti{ (ehyytia@snakemail.hut.fi).)
34 .\" Modified 1998-04-10, Nicola'O^ Lichtmaier <nick@debian.org>
35 .\" with contribution from Francesco Potorti <F.Potorti@cnuce.cnr.it>
36 .\" Modified 2003-11-15, aeb, added rand_r
38 .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi all rights reserved.
39 .\" Translated 1997-01-21, YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
40 .\" Updated 2000-09-24, Kentaro Shirakata <argrath@ub32.org>
41 .\" Updated 2005-03-15, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
42 .\" Updated 2008-08-08, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.05
44 .TH RAND 3 2008-08-29 "" "Linux Programmer's Manual"
47 .\"O rand, srand \- random number generator.
48 rand, srand \- Íð¿ô¤òÀ¸À®¤¹¤ë´Ø¿ô
52 .B #include <stdlib.h>
56 .BI "int rand_r(unsigned int *" seedp );
58 .BI "void srand(unsigned int " seed );
62 .\"O Feature Test Macro Requirements for glibc (see
63 .\"O .BR feature_test_macros (7)):
64 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
65 .RB ( feature_test_macros (7)
70 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
75 .\"O function returns a pseudo-random integer in
76 .\"O the range [0,\ \fBRAND_MAX\fR].
78 ´Ø¿ô¤Ï [0,\ \fBRAND_MAX\fR] ¤ÎÈϰϤε¿»÷Íð¿ôÀ°¿ô¤òÊÖ¤¹¡£
82 .\"O function sets its argument as the seed for a new
83 .\"O sequence of pseudo-random integers to be returned by
88 ´Ø¿ô¤Çºî¤é¤ì¤ëµ¿»÷Íð¿ôÀ°¿ô·ÏÎó¤Î¿·¤·¤¤¼ï¤È¤·¤Æ¡¢
89 ¤½¤Î°ú¤¿ô¤ÎÃͤò»ÈÍѤ¹¤ë¡£
90 .\"O These sequences are repeatable by calling
94 ¤³¤ì¤é¤Î´Ø¿ô¤ò»ÈÍѤ·¤Æºî¤é¤ì¤¿µ¿»÷Íð¿ô·ÏÎó¤Ï¡¢
98 ºÆ¸½¤¹¤ë¤³¤È¤¬²Äǽ¤Ç¤¢¤ë¡£
100 .\"O If no seed value is provided, the
102 .\"O function is automatically
103 .\"O seeded with a value of 1.
104 ¼ï¤ÎÃͤ¬Í¿¤¨¤é¤ì¤Ê¤¤¾ì¹ç¤Ë¤Ï¡¢
107 ¼«Æ°Åª¤Ë 1 ¤ò¼ï¤È¤¹¤ë¡£
111 .\"O is not reentrant or thread-safe, since it
112 .\"O uses hidden state that is modified on each call.
113 .\"O This might just be
114 .\"O the seed value to be used by the next call, or it might be something
116 .\"O In order to get reproducible behavior in a threaded
117 .\"O application, this state must be made explicit.
120 .\"O is supplied with a pointer to an
121 .\"O .IR "unsigned int" ,
122 .\"O to be used as state.
124 ´Ø¿ô¤ÏºÆÆþ²Äǽ (reentrant) ¤Ç¤Ï¤Ê¤¤¡¢¤Ä¤Þ¤ê¥¹¥ì¥Ã¥É¡¦¥»¡¼¥Õ¤Ç¤Ï¤Ê¤¤¡£
125 ¤Ê¤¼¤Ê¤é¡¢¤³¤Î´Ø¿ô¤Ï±£¤·¾õÂÖ¤ò»ý¤Ã¤Æ¤ª¤ê¡¢¸Æ¤Ó½Ð¤µ¤ì¤ëÅÙ¤ËÊѹ¹¤¹¤ë¤«¤é¤Ç¤¢¤ë¡£
126 ¤Á¤ç¤¦¤É¤³¤Î±£¤·¾õÂÖ¤¬¼¡¤Î¸Æ¤Ó½Ð¤·»þ¤ÎÍð¿ô¤Î¼ï¤È¤·¤Æ»È¤ï¤ì¤ë¤è¤¦¤Ê¤â¤Î¤Ç¤¢¤ë¡£
127 ¼ÂºÝ¤Ë¤Ï¤â¤¦¾¯¤·Ê£»¨¤«¤â¤·¤ì¤Ê¤¤¤¬¡£¥¹¥ì¥Ã¥É¤ò»ÈÍѤ¹¤ë¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ç
128 ºÆ¸½²Äǽ¤ÊÆ°ºî¤ò¤µ¤»¤¿¤¤¾ì¹ç¤Ë¤Ï¡¢¤³¤Î±£¤·¾õÂÖ¤¬¸¶°ø¤ÇºÆ¸½À¤¬¤Ê¤¯¤Ê¤ë¡£
132 ·¿¤ÎÊÑ¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤ò°ú¤¿ô¤Ë¤È¤ê¡¢
133 ¤³¤ÎÊÑ¿ô¤ò¾õÂÖ³ÊǼÍѤ˻ÈÍѤ¹¤ë¡£
134 .\"O This is a very small amount of state, so this function will be a weak
135 .\"O pseudo-random generator.
137 .\"O .BR drand48_r (3)
139 ¾õÂÖ¿ô¤¬Èó¾ï¤Ë¾®¤µ¤¤¤Î¤Ç¡¢¤³¤Î´Ø¿ô¤Ï¤¢¤Þ¤ê¤è¤¤µ¼»÷Íð¿ôÀ¸À®´ï¤È¤Ï¸À¤¨¤Ê¤¤¤À¤í¤¦¡£
143 .\"O .SH "RETURN VALUE"
149 .\"O functions return a value
154 .\"O function returns no value.
163 .\"O .SH "CONFORMING TO"
169 .\"O conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
172 .\"O is from POSIX.1-2001.
177 ¤Ï SVr4, 4.3BSD, C89, C99, POSIX.1-2001 ¤Ë½àµò¤·¤Æ¤¤¤ë¡£
180 ¤Ï POSIX.1-2001 ¤ËͳÍ褹¤ë¡£
181 .\"O POSIX.1-2008 marks
186 ¤òÇÑ»ßͽÄê¤È¤·¤Æ¤¤¤ë¡£
193 .\"O in the Linux C Library use
194 .\"O the same random number generator as
197 .\"O .BR srandom (3),
199 .\"O the lower-order bits should be as random as the higher-order bits.
203 ¤Î Linux C Library ÈǤϡ¢
207 ¤Îξ´Ø¿ô¤ÈƱ¤¸Íð¿ôÀ¸À®
208 ¥¢¥ë¥´¥ê¥º¥à¤ò»ÈÍѤ·¤Æ¤¤¤ë¡£¤½¤Î¤¿¤á¡¢²¼°Ì¤Î¥Ó¥Ã¥È¤Ï¾å°Ì¤Î¥Ó¥Ã¥È¤È
209 Ʊ¤¸¤¯¤é¤¤¤Ë¥é¥ó¥À¥à¤Ç¤¢¤ë¡£
210 .\"O However, on older
212 .\"O implementations, and on current implementations on different systems,
213 .\"O the lower-order bits are much less random than the higher-order bits.
214 .\"O Do not use this function in applications intended to be portable
215 .\"O when good randomness is needed.
221 ¤Î¼ÂÁõ¤ä¡¢Â¾¤Î¥·¥¹¥Æ¥à¤Î¸½ºß¤Î¼ÂÁõ¤Ç¤Ï¡¢²¼°Ì¤Î¥Ó¥Ã¥È¤¬¾å°Ì¤Î¥Ó¥Ã¥È¤Û¤É
222 ¥é¥ó¥À¥à¤Ë¤Ê¤Ã¤Æ¤¤¤Ê¤¤¡£°Ü¿¢À¤ò¹â¤á¤ë¾ì¹ç¤Ç¤â¡¢ÀºÅ٤ι⤤Íð¿ô¤¬É¬ÍפÊ
223 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ç¤Ï¤³¤Î´Ø¿ô¤Ï»ÈÍѤ·¤Æ¤Ï¤¤¤±¤Ê¤¤
229 .\"O POSIX.1-2001 gives the following example of an implementation of
233 .\"O possibly useful when one needs the same sequence on two different machines.
234 POSIX 1003.1-2003 ¤Ç¤Ï¡¢
238 ¤Î¼ÂÁõÎã¤È¤·¤Æ°Ê²¼¤òµó¤²¤Æ¤¤¤ë¡£¤³¤ì¤Ï¡¢°Û¤Ê¤ë£²¤Ä¤Î¥Þ¥·¥ó¤ÇƱ¤¸Íð¿ô·ÏÎó¤¬
239 ɬÍפʾì¹ç¤Ë¤ÏÊØÍø¤Ç¤¢¤í¤¦¡£
243 static unsigned long next = 1;
245 .\"O /* RAND_MAX assumed to be 32767 */
246 /* RAND_MAX ¤ò 32767 ¤È²¾Äê */
248 next = next * 1103515245 + 12345;
249 return((unsigned)(next/65536) % 32768);
252 void mysrand(unsigned seed) {