OSDN Git Service

(split) Convert release and draft pages to UTF-8.
[linuxjm/LDP_man-pages.git] / release / 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 .SH 名前
46 rand, srand \- 乱数を生成する関数
47 .SH 書式
48 .nf
49 .B #include <stdlib.h>
50 .sp
51 .B int rand(void);
52 .sp
53 .BI "int rand_r(unsigned int *" seedp );
54 .sp
55 .BI "void srand(unsigned int " seed );
56 .fi
57 .sp
58 .in -4n
59 glibc 向けの機能検査マクロの要件
60 .RB ( feature_test_macros (7)
61 参照):
62 .in
63 .sp
64 .BR rand_r ():
65 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
66 .SH 説明
67 .BR rand ()
68 関数は [0,\ \fBRAND_MAX\fR] の範囲の疑似乱数整数を返す。
69 .PP
70 .BR srand ()
71 関数は、
72 .BR rand ()
73 関数で作られる疑似乱数整数系列の新しい種として、
74 その引き数の値を使用する。
75 これらの関数を使用して作られた疑似乱数系列は、
76 同じ値を引き数として
77 .BR srand ()
78 を呼ぶことで、
79 再現することが可能である。
80 .PP
81 種の値が与えられない場合には、
82 .BR rand ()
83 関数は
84 自動的に 1 を種とする。
85 .PP
86 .BR rand ()
87 関数は再入可能 (reentrant) ではない、つまりスレッド・セーフではない。
88 なぜなら、この関数は隠し状態を持っており、呼び出される度に変更するからである。
89 ちょうどこの隠し状態が次の呼び出し時の乱数の種として使われるようなものである。
90 実際にはもう少し複雑かもしれないが。スレッドを使用するアプリケーションで
91 再現可能な動作をさせたい場合には、この隠し状態が原因で再現性がなくなる。
92 .BR rand_r ()
93 関数は
94 .I unsigned int
95 型の変数へのポインタを引き数にとり、
96 この変数を状態格納用に使用する。
97 状態数が非常に小さいので、この関数はあまりよい擬似乱数生成器とは言えないだろう。
98 代わりに
99 .BR drand48_r (3)
100 を使うとよい。
101 .SH 返り値
102 .BR rand ()
103
104 .BR rand_r ()
105 関数は 0 と
106 .B RAND_MAX
107 の間の数を返す。
108 .BR srand ()
109 関数は値を返さない。
110 .SH 準拠
111 関数
112 .BR rand ()
113
114 .BR srand ()
115 は SVr4, 4.3BSD, C89, C99, POSIX.1-2001 に準拠している。
116 関数
117 .BR rand_r ()
118 は POSIX.1-2001 に由来する。
119 POSIX.1-2008 は、
120 .BR rand_r ()
121 を廃止予定としている。
122 .SH 注意
123 .BR rand ()
124
125 .BR srand ()
126 の Linux C Library 版は、
127 .BR random (3)
128
129 .BR srandom (3)
130 の両関数と同じ乱数生成
131 アルゴリズムを使用している。そのため、下位のビットは上位のビットと
132 同じくらいにランダムである。
133 しかし、旧版の
134 .BR rand ()
135 の実装や、他のシステムの現在の実装では、下位のビットが上位のビットほど
136 ランダムになっていない。移植性を高める場合でも、精度の高い乱数が必要な
137 アプリケーションではこの関数は使用してはいけない
138 (代わりに
139 .BR random (3)
140 を使うこと)。
141 .SH 例
142 POSIX 1003.1-2003 では、
143 .BR rand ()
144
145 .BR srand ()
146 の実装例として以下を挙げている。これは、異なる2つのマシンで同じ乱数系列が
147 必要な場合には便利であろう。
148 .sp
149 .in +4n
150 .nf
151 static unsigned long next = 1;
152
153 /* RAND_MAX を 32767 と仮定 */
154 int myrand(void) {
155     next = next * 1103515245 + 12345;
156     return((unsigned)(next/65536) % 32768);
157 }
158
159 void mysrand(unsigned seed) {
160     next = seed;
161 }
162 .fi
163 .in
164 .SH 関連項目
165 .BR drand48 (3),
166 .BR random (3)