OSDN Git Service

(split) LDP_man-pages: release ioctl.2 [JM:00378]
[linuxjm/LDP_man-pages.git] / draft / man3 / rand.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 .\"
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
37 .\"
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
43 .\"
44 .TH RAND 3 2008-08-29 "" "Linux Programmer's Manual"
45 .\"O .SH NAME
46 .SH Ì¾Á°
47 .\"O rand, srand \- random number generator.
48 rand, srand \- Íð¿ô¤òÀ¸À®¤¹¤ë´Ø¿ô
49 .\"O .SH SYNOPSIS
50 .SH ½ñ¼°
51 .nf
52 .B #include <stdlib.h>
53 .sp
54 .B int rand(void);
55 .sp
56 .BI "int rand_r(unsigned int *" seedp );
57 .sp
58 .BI "void srand(unsigned int " seed );
59 .fi
60 .sp
61 .in -4n
62 .\"O Feature Test Macro Requirements for glibc (see
63 .\"O .BR feature_test_macros (7)):
64 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
65 .RB ( feature_test_macros (7)
66 »²¾È):
67 .in
68 .sp
69 .BR rand_r ():
70 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
71 .\"O .SH DESCRIPTION
72 .SH ÀâÌÀ
73 .\"O The
74 .\"O .BR rand ()
75 .\"O function returns a pseudo-random integer in
76 .\"O the range [0,\ \fBRAND_MAX\fR].
77 .BR rand ()
78 ´Ø¿ô¤Ï [0,\ \fBRAND_MAX\fR] ¤ÎÈϰϤε¿»÷Íð¿ôÀ°¿ô¤òÊÖ¤¹¡£
79 .PP
80 .\"O The
81 .\"O .BR srand ()
82 .\"O function sets its argument as the seed for a new
83 .\"O sequence of pseudo-random integers to be returned by
84 .\"O .BR rand ().
85 .BR srand ()
86 ´Ø¿ô¤Ï¡¢
87 .BR rand ()
88 ´Ø¿ô¤Çºî¤é¤ì¤ëµ¿»÷Íð¿ôÀ°¿ô·ÏÎó¤Î¿·¤·¤¤¼ï¤È¤·¤Æ¡¢
89 ¤½¤Î°ú¤­¿ô¤ÎÃͤò»ÈÍѤ¹¤ë¡£
90 .\"O These sequences are repeatable by calling
91 .\"O .BR srand ()
92 .\"O with the same
93 .\"O seed value.
94 ¤³¤ì¤é¤Î´Ø¿ô¤ò»ÈÍѤ·¤Æºî¤é¤ì¤¿µ¿»÷Íð¿ô·ÏÎó¤Ï¡¢
95 Ʊ¤¸Ãͤò°ú¤­¿ô¤È¤·¤Æ
96 .BR srand ()
97 ¤ò¸Æ¤Ö¤³¤È¤Ç¡¢
98 ºÆ¸½¤¹¤ë¤³¤È¤¬²Äǽ¤Ç¤¢¤ë¡£
99 .PP
100 .\"O If no seed value is provided, the
101 .\"O .BR rand ()
102 .\"O function is automatically
103 .\"O seeded with a value of 1.
104 ¼ï¤ÎÃͤ¬Í¿¤¨¤é¤ì¤Ê¤¤¾ì¹ç¤Ë¤Ï¡¢
105 .BR rand ()
106 ´Ø¿ô¤Ï
107 ¼«Æ°Åª¤Ë 1 ¤ò¼ï¤È¤¹¤ë¡£
108 .PP
109 .\"O The function
110 .\"O .BR rand ()
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
115 .\"O more elaborate.
116 .\"O In order to get reproducible behavior in a threaded
117 .\"O application, this state must be made explicit.
118 .\"O The function
119 .\"O .BR rand_r ()
120 .\"O is supplied with a pointer to an
121 .\"O .IR "unsigned int" ,
122 .\"O to be used as state.
123 .BR rand ()
124 ´Ø¿ô¤ÏºÆÆþ²Äǽ (reentrant) ¤Ç¤Ï¤Ê¤¤¡¢¤Ä¤Þ¤ê¥¹¥ì¥Ã¥É¡¦¥»¡¼¥Õ¤Ç¤Ï¤Ê¤¤¡£
125 ¤Ê¤¼¤Ê¤é¡¢¤³¤Î´Ø¿ô¤Ï±£¤·¾õÂÖ¤ò»ý¤Ã¤Æ¤ª¤ê¡¢¸Æ¤Ó½Ð¤µ¤ì¤ëÅÙ¤ËÊѹ¹¤¹¤ë¤«¤é¤Ç¤¢¤ë¡£
126 ¤Á¤ç¤¦¤É¤³¤Î±£¤·¾õÂÖ¤¬¼¡¤Î¸Æ¤Ó½Ð¤·»þ¤ÎÍð¿ô¤Î¼ï¤È¤·¤Æ»È¤ï¤ì¤ë¤è¤¦¤Ê¤â¤Î¤Ç¤¢¤ë¡£
127 ¼ÂºÝ¤Ë¤Ï¤â¤¦¾¯¤·Ê£»¨¤«¤â¤·¤ì¤Ê¤¤¤¬¡£¥¹¥ì¥Ã¥É¤ò»ÈÍѤ¹¤ë¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ç
128 ºÆ¸½²Äǽ¤ÊÆ°ºî¤ò¤µ¤»¤¿¤¤¾ì¹ç¤Ë¤Ï¡¢¤³¤Î±£¤·¾õÂÖ¤¬¸¶°ø¤ÇºÆ¸½À­¤¬¤Ê¤¯¤Ê¤ë¡£
129 .BR rand_r ()
130 ´Ø¿ô¤Ï
131 .I unsigned int
132 ·¿¤ÎÊÑ¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤ò°ú¤­¿ô¤Ë¤È¤ê¡¢
133 ¤³¤ÎÊÑ¿ô¤ò¾õÂÖ³ÊǼÍѤ˻ÈÍѤ¹¤ë¡£
134 .\"O This is a very small amount of state, so this function will be a weak
135 .\"O pseudo-random generator.
136 .\"O Try
137 .\"O .BR drand48_r (3)
138 .\"O instead.
139 ¾õÂÖ¿ô¤¬Èó¾ï¤Ë¾®¤µ¤¤¤Î¤Ç¡¢¤³¤Î´Ø¿ô¤Ï¤¢¤Þ¤ê¤è¤¤µ¼»÷Íð¿ôÀ¸À®´ï¤È¤Ï¸À¤¨¤Ê¤¤¤À¤í¤¦¡£
140 Âå¤ï¤ê¤Ë
141 .BR drand48_r (3)
142 ¤ò»È¤¦¤È¤è¤¤¡£
143 .\"O .SH "RETURN VALUE"
144 .SH ÊÖ¤êÃÍ
145 .\"O The
146 .\"O .BR rand ()
147 .\"O and
148 .\"O .BR rand_r ()
149 .\"O functions return a value
150 .\"O between 0 and
151 .\"O .BR RAND_MAX .
152 .\"O The
153 .\"O .BR srand ()
154 .\"O function returns no value.
155 .BR rand ()
156 ¤È
157 .BR rand_r ()
158 ´Ø¿ô¤Ï 0 ¤È
159 .B RAND_MAX
160 ¤Î´Ö¤Î¿ô¤òÊÖ¤¹¡£
161 .BR srand ()
162 ´Ø¿ô¤ÏÃͤòÊÖ¤µ¤Ê¤¤¡£
163 .\"O .SH "CONFORMING TO"
164 .SH ½àµò
165 .\"O The functions
166 .\"O .BR rand ()
167 .\"O and
168 .\"O .BR srand ()
169 .\"O conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
170 .\"O The function
171 .\"O .BR rand_r ()
172 .\"O is from POSIX.1-2001.
173 ´Ø¿ô
174 .BR rand ()
175 ¤È
176 .BR srand ()
177 ¤Ï SVr4, 4.3BSD, C89, C99, POSIX.1-2001 ¤Ë½àµò¤·¤Æ¤¤¤ë¡£
178 ´Ø¿ô
179 .BR rand_r ()
180 ¤Ï POSIX.1-2001 ¤ËͳÍ褹¤ë¡£
181 .\"O POSIX.1-2008 marks
182 .\"O .BR rand_r ()
183 .\"O as obsolete.
184 POSIX.1-2008 ¤Ï¡¢
185 .BR rand_r ()
186 ¤òÇÑ»ßͽÄê¤È¤·¤Æ¤¤¤ë¡£
187 .\"O .SH NOTES
188 .SH Ãí°Õ
189 .\"O The versions of
190 .\"O .BR rand ()
191 .\"O and
192 .\"O .BR srand ()
193 .\"O in the Linux C Library use
194 .\"O the same random number generator as
195 .\"O .BR random (3)
196 .\"O and
197 .\"O .BR srandom (3),
198 .\"O so
199 .\"O the lower-order bits should be as random as the higher-order bits.
200 .BR rand ()
201 ¤È
202 .BR srand ()
203 ¤Î Linux C Library ÈǤϡ¢
204 .BR random (3)
205 ¤È
206 .BR srandom (3)
207 ¤Îξ´Ø¿ô¤ÈƱ¤¸Íð¿ôÀ¸À®
208 ¥¢¥ë¥´¥ê¥º¥à¤ò»ÈÍѤ·¤Æ¤¤¤ë¡£¤½¤Î¤¿¤á¡¢²¼°Ì¤Î¥Ó¥Ã¥È¤Ï¾å°Ì¤Î¥Ó¥Ã¥È¤È
209 Ʊ¤¸¤¯¤é¤¤¤Ë¥é¥ó¥À¥à¤Ç¤¢¤ë¡£
210 .\"O However, on older
211 .\"O .BR rand ()
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.
216 .\"O (Use
217 .\"O .BR random (3)
218 .\"O instead.)
219 ¤·¤«¤·¡¢µìÈǤÎ
220 .BR rand ()
221 ¤Î¼ÂÁõ¤ä¡¢Â¾¤Î¥·¥¹¥Æ¥à¤Î¸½ºß¤Î¼ÂÁõ¤Ç¤Ï¡¢²¼°Ì¤Î¥Ó¥Ã¥È¤¬¾å°Ì¤Î¥Ó¥Ã¥È¤Û¤É
222 ¥é¥ó¥À¥à¤Ë¤Ê¤Ã¤Æ¤¤¤Ê¤¤¡£°Ü¿¢À­¤ò¹â¤á¤ë¾ì¹ç¤Ç¤â¡¢ÀºÅ٤ι⤤Íð¿ô¤¬É¬ÍפÊ
223 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ç¤Ï¤³¤Î´Ø¿ô¤Ï»ÈÍѤ·¤Æ¤Ï¤¤¤±¤Ê¤¤
224 (Âå¤ï¤ê¤Ë
225 .BR random (3)
226 ¤ò»È¤¦¤³¤È)¡£
227 .\"O .SH EXAMPLE
228 .SH Îã
229 .\"O POSIX.1-2001 gives the following example of an implementation of
230 .\"O .BR rand ()
231 .\"O and
232 .\"O .BR srand (),
233 .\"O possibly useful when one needs the same sequence on two different machines.
234 POSIX 1003.1-2003 ¤Ç¤Ï¡¢
235 .BR rand ()
236 ¤È
237 .BR srand ()
238 ¤Î¼ÂÁõÎã¤È¤·¤Æ°Ê²¼¤òµó¤²¤Æ¤¤¤ë¡£¤³¤ì¤Ï¡¢°Û¤Ê¤ë£²¤Ä¤Î¥Þ¥·¥ó¤ÇƱ¤¸Íð¿ô·ÏÎó¤¬
239 ɬÍפʾì¹ç¤Ë¤ÏÊØÍø¤Ç¤¢¤í¤¦¡£
240 .sp
241 .in +4n
242 .nf
243 static unsigned long next = 1;
244
245 .\"O /* RAND_MAX assumed to be 32767 */
246 /* RAND_MAX ¤ò 32767 ¤È²¾Äê */
247 int myrand(void) {
248     next = next * 1103515245 + 12345;
249     return((unsigned)(next/65536) % 32768);
250 }
251
252 void mysrand(unsigned seed) {
253     next = seed;
254 }
255 .fi
256 .in
257 .\"O .SH "SEE ALSO"
258 .SH ´ØÏ¢¹àÌÜ
259 .BR drand48 (3),
260 .BR random (3)