OSDN Git Service

(split) LDP: Update drafts based on LDP v3.40.
[linuxjm/LDP_man-pages.git] / draft / man3 / rand.3
index adbbe04..52a5bbb 100644 (file)
 .\" 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
 .\"
-.\" Japanese Version Copyright (c) 1997 YOSHINO Takashi all rights reserved.
-.\" Translated 1997-01-21, YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
-.\" Updated 2000-09-24, Kentaro Shirakata <argrath@ub32.org>
-.\" 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
-.SH Ì¾Á°
-.\"O rand, srand \- random number generator.
-rand, srand \- Íð¿ô¤òÀ¸À®¤¹¤ë´Ø¿ô
-.\"O .SH SYNOPSIS
-.SH ½ñ¼°
+.\" This file was generated with po4a. Translate the source file.
+.\"
+.\"*******************************************************************
+.TH RAND 3 2010\-10\-01 "" "Linux Programmer's Manual"
+.SH 名前
+rand, srand \- 乱数を生成する関数
+.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
-.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_r\fP(): _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
+.SH 説明
+\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) ¤Ç¤Ï¤Ê¤¤¡¢¤Ä¤Þ¤ê¥¹¥ì¥Ã¥É¡¦¥»¡¼¥Õ¤Ç¤Ï¤Ê¤¤¡£
-¤Ê¤¼¤Ê¤é¡¢¤³¤Î´Ø¿ô¤Ï±£¤·¾õÂÖ¤ò»ý¤Ã¤Æ¤ª¤ê¡¢¸Æ¤Ó½Ð¤µ¤ì¤ëÅÙ¤ËÊѹ¹¤¹¤ë¤«¤é¤Ç¤¢¤ë¡£
-¤Á¤ç¤¦¤É¤³¤Î±£¤·¾õÂÖ¤¬¼¡¤Î¸Æ¤Ó½Ð¤·»þ¤ÎÍð¿ô¤Î¼ï¤È¤·¤Æ»È¤ï¤ì¤ë¤è¤¦¤Ê¤â¤Î¤Ç¤¢¤ë¡£
-¼ÂºÝ¤Ë¤Ï¤â¤¦¾¯¤·Ê£»¨¤«¤â¤·¤ì¤Ê¤¤¤¬¡£¥¹¥ì¥Ã¥É¤ò»ÈÍѤ¹¤ë¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ç
-ºÆ¸½²Äǽ¤ÊÆ°ºî¤ò¤µ¤»¤¿¤¤¾ì¹ç¤Ë¤Ï¡¢¤³¤Î±£¤·¾õÂÖ¤¬¸¶°ø¤ÇºÆ¸½À­¤¬¤Ê¤¯¤Ê¤ë¡£
-.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"
-.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"
-.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 ()
-¤òÇÑ»ßͽÄê¤È¤·¤Æ¤¤¤ë¡£
-.\"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
-.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 ()
-¤Î¼ÂÁõÎã¤È¤·¤Æ°Ê²¼¤òµó¤²¤Æ¤¤¤ë¡£¤³¤ì¤Ï¡¢°Û¤Ê¤ë£²¤Ä¤Î¥Þ¥·¥ó¤ÇƱ¤¸Íð¿ô·ÏÎó¤¬
-ɬÍפʾì¹ç¤Ë¤ÏÊØÍø¤Ç¤¢¤í¤¦¡£
+\fBrand\fP() 関数はリ再入可能 (reentrant) ではない、つまりスレッド・セーフではない。
+この関数には隠し状態があり、呼び出される度にこの隠し状態が変更されるからである。
+ちょうどこの隠し状態が次の呼び出し時の乱数の種として使われるようなものである。
+実際にはもう少し複雑かもしれないが。
+スレッドを使用するアプリケーションで再現可能な動作をさせたい場合には、
+この状態を明示的に指定できなければならない。これを行うには、
+再入可能な \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 返り値
+関数 \fBrand\fP() と \fBrand_r\fP() は 0 以上 \fBRAND_MAX\fP 以下の数を返す。
+関数 \fBsrand\fP() は値を返さない。
+.SH 準拠
+関数 \fBrand\fP()  と \fBsrand\fP()  は SVr4, 4.3BSD, C89, C99, POSIX.1\-2001 に準拠している。
+関数 \fBrand_r\fP()  は POSIX.1\-2001 に由来する。 POSIX.1\-2008 は、 \fBrand_r\fP()
+を廃止予定としている。
+.SH 注意
+\fBrand\fP()  と \fBsrand\fP()  の Linux C Library 版は、 \fBrandom\fP(3)  と \fBsrandom\fP(3)
+の両関数と同じ乱数生成 アルゴリズムを使用している。そのため、下位のビットは上位のビットと 同じくらいにランダムである。 しかし、旧版の
+\fBrand\fP()  の実装や、他のシステムの現在の実装では、下位のビットが上位のビットほど
+ランダムになっていない。移植性を高める場合でも、精度の高い乱数が必要な アプリケーションではこの関数は使用してはいけない (代わりに
+\fBrandom\fP(3)  を使うこと)。
+.SH 例
+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 ¤È²¾Äê */
+/* RAND_MAX を 32767 と仮定 */
 int myrand(void) {
     next = next * 1103515245 + 12345;
     return((unsigned)(next/65536) % 32768);
@@ -254,7 +118,42 @@ void mysrand(unsigned seed) {
 }
 .fi
 .in
-.\"O .SH "SEE ALSO"
-.SH ´ØÏ¢¹àÌÜ
-.BR drand48 (3),
-.BR random (3)
+.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 関連項目
+\fBdrand48\fP(3), \fBrandom\fP(3)
+.SH この文書について
+この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.40 の一部
+である。プロジェクトの説明とバグ報告に関する情報は
+http://www.kernel.org/doc/man\-pages/ に書かれている。