OSDN Git Service

(split) Convert release and draft pages to UTF-8.
[linuxjm/LDP_man-pages.git] / release / 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  2010-09-20 "GNU" "Linux Programmer's Manual"
40 .SH 名前
41 random, srandom, initstate, setstate \- 乱数を生成する関数
42 .SH 書式
43 .nf
44 .B #include <stdlib.h>
45 .sp
46 .B long int random(void);
47 .br
48 .BI "void srandom(unsigned int " seed );
49
50 .BI "char *initstate(unsigned int " seed ", char *" state ", size_t " n );
51
52 .BI "char *setstate(char *" state );
53 .fi
54 .sp
55 .in -4n
56 glibc 向けの機能検査マクロの要件
57 .RB ( feature_test_macros (7)
58 参照):
59 .in
60 .sp
61 .ad l
62 .BR random (),
63 .BR srandom (),
64 .BR initstate (),
65 .BR setstate ():
66 .RS 4
67 _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
68 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
69 .RE
70 .ad
71 .SH 説明
72 .BR random ()
73 関数は、非線形加法フィードバックを用いた乱数生成関数である。
74 この関数は、0 から \fBRAND_MAX\fR までの疑似乱数を返す。
75 そのために 31 個のロング整数からなるデフォルトの表を使用する。
76 この乱数を生成する関数の周期はとても長く、およそ
77 .I "16\ *\ ((2^31)\ \-\ 1)"
78 である。
79 .PP
80 .BR srandom ()
81 関数は、
82 .BR random ()
83 で返される疑似乱数整数系列の種を設定する。
84 そのためには新しい種を引数にして
85 .BR srandom ()
86 を呼べばよい。
87 .BR random ()
88 で生成される系列は、
89 引数に同じ種の値を用いて
90 .BR srandom ()
91 を呼ぶことで再現可能である。
92 種の値が与えられない場合には
93 .BR random ()
94 関数は、自動的に 1 を種に設定する。
95 .PP
96 .BR initstate ()
97 関数は、
98 .BR random ()
99 で使用される状態配列
100 \fIstate\fP を初期化する。
101 .BR initstate ()
102 では、状態配列の大きさ \fIn\fP は
103 使用する関数の乱数生成の性能の程度を決定するために使用される
104 \(em 状態配列が大きい程、乱数の性能はよくなる。
105 \fIseed\fP は初期化のための種である。
106 これは乱数系列の開始位置を決定するものであり、
107 この値を指定することで同一の開始位置から乱数の生成を
108 再開することができる。
109 .PP
110 .BR setstate ()
111 関数は、
112 .BR random ()
113 で使用される状態配列を変更する。
114 状態配列 \fIstate\fP は、
115 .BR initstate ()
116 または
117 .BR setstate ()
118
119 次に呼び出されるまで、乱数の生成に使用される。
120 \fIstate\fP は
121 .BR initstate ()
122 を用いて最初に初期化されているか、
123 以前に呼び出した
124 .BR setstate ()
125 の結果でなければならない。
126 .SH 返り値
127 .BR random ()
128 関数は 0 と
129 .B RAND_MAX
130 の間の値を返す。
131 .BR srandom ()
132 関数は値を返さない。
133 .BR initstate ()
134 関数は直前の状態配列へのポインタを返す。
135 .BR setstate ()
136 関数は直前の状態配列へのポインタを返し、
137 エラーの場合は NULL を返す。
138 .SH エラー
139 .TP
140 .B EINVAL
141 .BR initstate ()
142 で8バイトよりも小さい状態配列を指定した。
143 .SH 準拠
144 4.3BSD, POSIX.1-2001.
145 .SH 注意
146 状態配列 \fIn\fP の大きさの現在の「最適」値は
147 8、32、64、128、256 バイトである。
148 その他の量を指定した場合には、指定した量を越えない上述の値に
149 最も近い値になる。
150 8 バイト未満の量を指定した場合にはエラーの原因となる。
151
152 複数のスレッドが
153 .BR random ()
154 を使うような状況では、この関数を使用すべきではない。
155 その場合には
156 .BR random_r (3)
157 を使うこと。
158
159 乱数の生成は複雑な話題である。
160 .I Numerical Recipes in C: The Art of Scientific Computing
161 (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William
162 T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.)
163 では実用的な乱数生成を論点とした優れた議論が第 7 章 (乱数) で展開されている。
164
165 より理論的な議論については Donald E. Knuth の
166 .IR "The Art of Computer Programming" ,
167 volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
168 Addison-Wesley Publishing Company, 1981
169 の第 3 章 (乱数) を見よ。ここでは、
170 たくさんの実用的な話題についても深く網羅されている。
171 .SH 関連項目
172 .BR drand48 (3),
173 .BR rand (3),
174 .BR random_r (3),
175 .BR srand (3)