OSDN Git Service

c999c5b28a8d015e647a6291869a0f5642bee402
[linuxjm/LDP_man-pages.git] / release / man3 / rand.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References consulted:
26 .\"     Linux libc source code
27 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\"     386BSD man pages
29 .\"
30 .\" Modified 1993-03-29, David Metcalfe
31 .\" Modified 1993-04-28, Lars Wirzenius
32 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
33 .\" Modified 1995-05-18, Rik Faith (faith@cs.unc.edu) to add
34 .\"          better discussion of problems with rand on other systems.
35 .\"          (Thanks to Esa Hyyti{ (ehyytia@snakemail.hut.fi).)
36 .\" Modified 1998-04-10, Nicolás Lichtmaier <nick@debian.org>
37 .\"          with contribution from Francesco Potorti <F.Potorti@cnuce.cnr.it>
38 .\" Modified 2003-11-15, aeb, added rand_r
39 .\" 2010-09-13, mtk, added example program
40 .\"
41 .\"*******************************************************************
42 .\"
43 .\" This file was generated with po4a. Translate the source file.
44 .\"
45 .\"*******************************************************************
46 .TH RAND 3 2010\-10\-01 "" "Linux Programmer's Manual"
47 .SH 名前
48 rand, srand \- 乱数を生成する関数
49 .SH 書式
50 .nf
51 \fB#include <stdlib.h>\fP
52 .sp
53 \fBint rand(void);\fP
54 .sp
55 \fBint rand_r(unsigned int *\fP\fIseedp\fP\fB);\fP
56 .sp
57 \fBvoid srand(unsigned int \fP\fIseed\fP\fB);\fP
58 .fi
59 .sp
60 .in -4n
61 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
62 .in
63 .sp
64 \fBrand_r\fP(): _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
65 .SH 説明
66 \fBrand\fP() 関数は 0 以上 \fBRAND_MAX\fP 以下
67 (数学的に書くと [0,\ \fBRAND_MAX\fP]) の範囲の疑似乱数整数を返す。
68 .PP
69 \fBsrand\fP()  関数は、 \fBrand\fP()  関数で作られる疑似乱数整数系列の新しい種として、 その引き数の値を使用する。
70 これらの関数を使用して作られた疑似乱数系列は、 同じ値を引き数として \fBsrand\fP()  を呼ぶことで、 再現することが可能である。
71 .PP
72 種の値が与えられない場合には、 \fBrand\fP()  関数は 自動的に 1 を種とする。
73 .PP
74 \fBrand\fP() 関数は再入可能 (reentrant) ではない、つまりスレッド・セーフではない。
75 この関数には隠し状態があり、呼び出される度にこの隠し状態が変更されるからである。
76 ちょうどこの隠し状態が次の呼び出し時の乱数の種として使われるようなものである。
77 実際にはもう少し複雑かもしれないが。
78 スレッドを使用するアプリケーションで再現可能な動作をさせたい場合には、
79 この状態を明示的に指定できなければならない。これを行うには、
80 再入可能な \fBrand_r\fP() 関数を使用する。
81
82 \fBrand\fP() と同様、 \fBrand_r\fP() は [0,\ \fBRAND_MAX\fP] の範囲の疑似乱数整数を返す。
83 \fIseedp\fP 引き数は、\fBrand_r\fP() の呼び出し間で状態を保持するために使用される
84 \fIunsigned int\fP へのポインタである。
85 \fIseedp\fP が指す整数に同じ初期値を \fBrand_r\fP() を呼び出し、
86 呼び出し間でその値が変更されなければ、同じ疑似乱数系列が得られる。
87
88 \fBrand_r\fP() の \fIseedp\fP 引き数が指す値により提供される状態は非常に小さな空間
89 なので、この関数は弱い疑似乱数生成器になってしまう。
90 代わりに \fBdrand48_r\fP(3) を使ってみるとよい。
91 .SH 返り値
92 関数 \fBrand\fP() と \fBrand_r\fP() は 0 以上 \fBRAND_MAX\fP 以下の数を返す。
93 関数 \fBsrand\fP() は値を返さない。
94 .SH 準拠
95 関数 \fBrand\fP()  と \fBsrand\fP()  は SVr4, 4.3BSD, C89, C99, POSIX.1\-2001 に準拠している。
96 関数 \fBrand_r\fP()  は POSIX.1\-2001 に由来する。 POSIX.1\-2008 は、 \fBrand_r\fP()
97 を廃止予定としている。
98 .SH 注意
99 \fBrand\fP()  と \fBsrand\fP()  の Linux C Library 版は、 \fBrandom\fP(3)  と \fBsrandom\fP(3)
100 の両関数と同じ乱数生成 アルゴリズムを使用している。そのため、下位のビットは上位のビットと 同じくらいにランダムである。 しかし、旧版の
101 \fBrand\fP()  の実装や、他のシステムの現在の実装では、下位のビットが上位のビットほど
102 ランダムになっていない。移植性を高める場合でも、精度の高い乱数が必要な アプリケーションではこの関数は使用してはいけない (代わりに
103 \fBrandom\fP(3)  を使うこと)。
104 .SH 例
105 POSIX 1003.1\-2003 では、 \fBrand\fP()  と \fBsrand\fP()
106 の実装例として以下を挙げている。これは、異なる2つのマシンで同じ乱数系列が 必要な場合には便利であろう。
107 .sp
108 .in +4n
109 .nf
110 static unsigned long next = 1;
111
112 /* RAND_MAX を 32767 と仮定 */
113 int myrand(void) {
114     next = next * 1103515245 + 12345;
115     return((unsigned)(next/65536) % 32768);
116 }
117
118 void mysrand(unsigned seed) {
119     next = seed;
120 }
121 .fi
122 .in
123 .PP
124 以下のプログラムを使うと、特定の乱数の種が与えられた場合に
125 \fBrand\fP() が生成する疑似乱数系列を表示することができる。
126 .in +4n
127 .nf
128
129 #include <stdlib.h>
130 #include <stdio.h>
131
132 int
133 main(int argc, char *argv[])
134 {
135     int j, r, nloops;
136     unsigned int seed;
137
138     if (argc != 3) {
139         fprintf(stderr, "Usage: %s <seed> <nloops>\en", argv[0]);
140         exit(EXIT_FAILURE);
141     }
142
143     seed = atoi(argv[1]);
144     nloops = atoi(argv[2]);
145
146     srand(seed);
147     for (j = 0; j < nloops; j++) {
148         r =  rand();
149         printf("%d\en", r);
150     }
151
152     exit(EXIT_SUCCESS);
153 }
154 .fi
155 .in
156 .SH 関連項目
157 \fBdrand48\fP(3), \fBrandom\fP(3)
158 .SH この文書について
159 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.52 の一部
160 である。プロジェクトの説明とバグ報告に関する情報は
161 http://www.kernel.org/doc/man\-pages/ に書かれている。