.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
.\"
+.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
.\"
.\" References consulted:
.\" Linux libc source code
.\" Modified 1995-05-18, Rik Faith (faith@cs.unc.edu) to add
.\" better discussion of problems with rand on other systems.
.\" (Thanks to Esa Hyyti{ (ehyytia@snakemail.hut.fi).)
-.\" Modified 1998-04-10, Nicola'O^ Lichtmaier <nick@debian.org>
+.\" Modified 1998-04-10, Nicolás Lichtmaier <nick@debian.org>
.\" with contribution from Francesco Potorti <F.Potorti@cnuce.cnr.it>
.\" Modified 2003-11-15, aeb, added rand_r
+.\" 2010-09-13, mtk, added example program
+.\"
+.\"*******************************************************************
+.\"
+.\" This file was generated with po4a. Translate the source file.
+.\"
+.\"*******************************************************************
.\"
.\" Japanese Version Copyright (c) 1997 YOSHINO Takashi all rights reserved.
.\" Translated 1997-01-21, YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
.\" Updated 2005-03-15, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
.\" Updated 2008-08-08, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.05
.\"
-.TH RAND 3 2008-08-29 "" "Linux Programmer's Manual"
-.\"O .SH NAME
+.TH RAND 3 2014\-01\-18 "" "Linux Programmer's Manual"
.SH 名前
-.\"O rand, srand \- random number generator.
rand, srand \- 乱数を生成する関数
-.\"O .SH SYNOPSIS
.SH 書式
.nf
-.B #include <stdlib.h>
+\fB#include <stdlib.h>\fP
.sp
-.B int rand(void);
+\fBint rand(void);\fP
.sp
-.BI "int rand_r(unsigned int *" seedp );
+\fBint rand_r(unsigned int *\fP\fIseedp\fP\fB);\fP
.sp
-.BI "void srand(unsigned int " seed );
+\fBvoid srand(unsigned int \fP\fIseed\fP\fB);\fP
.fi
.sp
.in -4n
-.\"O Feature Test Macro Requirements for glibc (see
-.\"O .BR feature_test_macros (7)):
-glibc 向けの機能検査マクロの要件
-.RB ( feature_test_macros (7)
-参照):
+glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照):
.in
.sp
-.BR rand_r ():
-_POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
-.\"O .SH DESCRIPTION
+\fBrand_r\fP(): _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
.SH 説明
-.\"O The
-.\"O .BR rand ()
-.\"O function returns a pseudo-random integer in
-.\"O the range [0,\ \fBRAND_MAX\fR].
-.BR rand ()
-関数は [0,\ \fBRAND_MAX\fR] の範囲の疑似乱数整数を返す。
+\fBrand\fP() 関数は 0 以上 \fBRAND_MAX\fP 以下
+(数学的に書くと [0,\ \fBRAND_MAX\fP]) の範囲の疑似乱数整数を返す。
.PP
-.\"O The
-.\"O .BR srand ()
-.\"O function sets its argument as the seed for a new
-.\"O sequence of pseudo-random integers to be returned by
-.\"O .BR rand ().
-.BR srand ()
-関数は、
-.BR rand ()
-関数で作られる疑似乱数整数系列の新しい種として、
-その引き数の値を使用する。
-.\"O These sequences are repeatable by calling
-.\"O .BR srand ()
-.\"O with the same
-.\"O seed value.
-これらの関数を使用して作られた疑似乱数系列は、
-同じ値を引き数として
-.BR srand ()
-を呼ぶことで、
-再現することが可能である。
+\fBsrand\fP() 関数は、 \fBrand\fP() 関数で作られる疑似乱数整数系列の新しい種として、 その引き数の値を使用する。
+これらの関数を使用して作られた疑似乱数系列は、 同じ値を引き数として \fBsrand\fP() を呼ぶことで、 再現することが可能である。
.PP
-.\"O If no seed value is provided, the
-.\"O .BR rand ()
-.\"O function is automatically
-.\"O seeded with a value of 1.
-種の値が与えられない場合には、
-.BR rand ()
-関数は
-自動的に 1 を種とする。
+種の値が与えられない場合には、 \fBrand\fP() 関数は 自動的に 1 を種とする。
.PP
-.\"O The function
-.\"O .BR rand ()
-.\"O is not reentrant or thread-safe, since it
-.\"O uses hidden state that is modified on each call.
-.\"O This might just be
-.\"O the seed value to be used by the next call, or it might be something
-.\"O more elaborate.
-.\"O In order to get reproducible behavior in a threaded
-.\"O application, this state must be made explicit.
-.\"O The function
-.\"O .BR rand_r ()
-.\"O is supplied with a pointer to an
-.\"O .IR "unsigned int" ,
-.\"O to be used as state.
-.BR rand ()
-関数は再入可能 (reentrant) ではない、つまりスレッド・セーフではない。
-なぜなら、この関数は隠し状態を持っており、呼び出される度に変更するからである。
+\fBrand\fP() 関数は再入可能 (reentrant) ではない、つまりスレッドセーフではない。
+この関数には隠し状態があり、呼び出される度にこの隠し状態が変更されるからである。
ちょうどこの隠し状態が次の呼び出し時の乱数の種として使われるようなものである。
-実際にはもう少し複雑かもしれないが。スレッドを使用するアプリケーションで
-再現可能な動作をさせたい場合には、この隠し状態が原因で再現性がなくなる。
-.BR rand_r ()
-関数は
-.I unsigned int
-型の変数へのポインタを引き数にとり、
-この変数を状態格納用に使用する。
-.\"O This is a very small amount of state, so this function will be a weak
-.\"O pseudo-random generator.
-.\"O Try
-.\"O .BR drand48_r (3)
-.\"O instead.
-状態数が非常に小さいので、この関数はあまりよい擬似乱数生成器とは言えないだろう。
-代わりに
-.BR drand48_r (3)
-を使うとよい。
-.\"O .SH "RETURN VALUE"
+実際にはもう少し複雑かもしれないが。
+スレッドを使用するアプリケーションで再現可能な動作をさせたい場合には、
+この状態を明示的に指定できなければならない。これを行うには、
+再入可能な \fBrand_r\fP() 関数を使用する。
+
+\fBrand\fP() と同様、 \fBrand_r\fP() は [0,\ \fBRAND_MAX\fP] の範囲の疑似乱数整数を返す。
+\fIseedp\fP 引き数は、\fBrand_r\fP() の呼び出し間で状態を保持するために使用される
+\fIunsigned int\fP へのポインターである。
+\fIseedp\fP が指す整数に同じ初期値を \fBrand_r\fP() を呼び出し、
+呼び出し間でその値が変更されなければ、同じ疑似乱数系列が得られる。
+
+\fBrand_r\fP() の \fIseedp\fP 引き数が指す値により提供される状態は非常に小さな空間
+なので、この関数は弱い疑似乱数生成器になってしまう。
+代わりに \fBdrand48_r\fP(3) を使ってみるとよい。
.SH 返り値
-.\"O The
-.\"O .BR rand ()
-.\"O and
-.\"O .BR rand_r ()
-.\"O functions return a value
-.\"O between 0 and
-.\"O .BR RAND_MAX .
-.\"O The
-.\"O .BR srand ()
-.\"O function returns no value.
-.BR rand ()
-と
-.BR rand_r ()
-関数は 0 と
-.B RAND_MAX
-の間の数を返す。
-.BR srand ()
-関数は値を返さない。
-.\"O .SH "CONFORMING TO"
+関数 \fBrand\fP() と \fBrand_r\fP() は 0 以上 \fBRAND_MAX\fP 以下の数を返す。
+関数 \fBsrand\fP() は値を返さない。
.SH 準拠
-.\"O The functions
-.\"O .BR rand ()
-.\"O and
-.\"O .BR srand ()
-.\"O conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
-.\"O The function
-.\"O .BR rand_r ()
-.\"O is from POSIX.1-2001.
-関数
-.BR rand ()
-と
-.BR srand ()
-は SVr4, 4.3BSD, C89, C99, POSIX.1-2001 に準拠している。
-関数
-.BR rand_r ()
-は POSIX.1-2001 に由来する。
-.\"O POSIX.1-2008 marks
-.\"O .BR rand_r ()
-.\"O as obsolete.
-POSIX.1-2008 は、
-.BR rand_r ()
+関数 \fBrand\fP() と \fBsrand\fP() は SVr4, 4.3BSD, C89, C99, POSIX.1\-2001 に準拠している。
+関数 \fBrand_r\fP() は POSIX.1\-2001 に由来する。 POSIX.1\-2008 は、 \fBrand_r\fP()
を廃止予定としている。
-.\"O .SH NOTES
.SH 注意
-.\"O The versions of
-.\"O .BR rand ()
-.\"O and
-.\"O .BR srand ()
-.\"O in the Linux C Library use
-.\"O the same random number generator as
-.\"O .BR random (3)
-.\"O and
-.\"O .BR srandom (3),
-.\"O so
-.\"O the lower-order bits should be as random as the higher-order bits.
-.BR rand ()
-と
-.BR srand ()
-の Linux C Library 版は、
-.BR random (3)
-と
-.BR srandom (3)
-の両関数と同じ乱数生成
-アルゴリズムを使用している。そのため、下位のビットは上位のビットと
-同じくらいにランダムである。
-.\"O However, on older
-.\"O .BR rand ()
-.\"O implementations, and on current implementations on different systems,
-.\"O the lower-order bits are much less random than the higher-order bits.
-.\"O Do not use this function in applications intended to be portable
-.\"O when good randomness is needed.
-.\"O (Use
-.\"O .BR random (3)
-.\"O instead.)
-しかし、旧版の
-.BR rand ()
-の実装や、他のシステムの現在の実装では、下位のビットが上位のビットほど
-ランダムになっていない。移植性を高める場合でも、精度の高い乱数が必要な
-アプリケーションではこの関数は使用してはいけない
-(代わりに
-.BR random (3)
-を使うこと)。
-.\"O .SH EXAMPLE
+\fBrand\fP() と \fBsrand\fP() の Linux C Library 版は、 \fBrandom\fP(3) と \fBsrandom\fP(3)
+の両関数と同じ乱数生成 アルゴリズムを使用している。そのため、下位のビットは上位のビットと 同じくらいにランダムである。 しかし、旧版の
+\fBrand\fP() の実装や、他のシステムの現在の実装では、下位のビットが上位のビットほど
+ランダムになっていない。移植性を高める場合でも、精度の高い乱数が必要な アプリケーションではこの関数は使用してはいけない (代わりに
+\fBrandom\fP(3) を使うこと)。
.SH 例
-.\"O POSIX.1-2001 gives the following example of an implementation of
-.\"O .BR rand ()
-.\"O and
-.\"O .BR srand (),
-.\"O possibly useful when one needs the same sequence on two different machines.
-POSIX 1003.1-2003 では、
-.BR rand ()
-と
-.BR srand ()
-の実装例として以下を挙げている。これは、異なる2つのマシンで同じ乱数系列が
-必要な場合には便利であろう。
+POSIX 1003.1\-2003 では、 \fBrand\fP() と \fBsrand\fP()
+の実装例として以下を挙げている。これは、異なる2つのマシンで同じ乱数系列が 必要な場合には便利であろう。
.sp
.in +4n
.nf
static unsigned long next = 1;
-.\"O /* RAND_MAX assumed to be 32767 */
/* RAND_MAX を 32767 と仮定 */
int myrand(void) {
next = next * 1103515245 + 12345;
return((unsigned)(next/65536) % 32768);
}
-void mysrand(unsigned seed) {
+void mysrand(unsigned int seed) {
next = seed;
}
.fi
.in
-.\"O .SH "SEE ALSO"
+.PP
+以下のプログラムを使うと、特定の乱数の種が与えられた場合に
+\fBrand\fP() が生成する疑似乱数系列を表示することができる。
+.in +4n
+.nf
+
+#include <stdlib.h>
+#include <stdio.h>
+
+int
+main(int argc, char *argv[])
+{
+ int j, r, nloops;
+ unsigned int seed;
+
+ if (argc != 3) {
+ fprintf(stderr, "Usage: %s <seed> <nloops>\en", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ seed = atoi(argv[1]);
+ nloops = atoi(argv[2]);
+
+ srand(seed);
+ for (j = 0; j < nloops; j++) {
+ r = rand();
+ printf("%d\en", r);
+ }
+
+ exit(EXIT_SUCCESS);
+}
+.fi
+.in
.SH 関連項目
-.BR drand48 (3),
-.BR random (3)
+\fBdrand48\fP(3), \fBrandom\fP(3)
+.SH この文書について
+この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部
+である。プロジェクトの説明とバグ報告に関する情報は
+http://www.kernel.org/doc/man\-pages/ に書かれている。