.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) .\" .\" 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. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" .\" References consulted: .\" Linux libc source code .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) .\" 386BSD man pages .\" .\" Modified 1993-03-29, David Metcalfe .\" Modified 1993-04-28, Lars Wirzenius .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu) .\" 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 .\" with contribution from Francesco Potorti .\" Modified 2003-11-15, aeb, added rand_r .\" .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi all rights reserved. .\" Translated 1997-01-21, YOSHINO Takashi .\" Updated 2000-09-24, Kentaro Shirakata .\" Updated 2005-03-15, Akihiro MOTOKI .\" Updated 2008-08-08, Akihiro MOTOKI , 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 書式 .nf .B #include .sp .B int rand(void); .sp .BI "int rand_r(unsigned int *" seedp ); .sp .BI "void srand(unsigned int " seed ); .fi .sp .in -4n .\"O Feature Test Macro Requirements for glibc (see .\"O .BR feature_test_macros (7)): glibc 向けの機能検査マクロの要件 .RB ( feature_test_macros (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] の範囲の疑似乱数整数を返す。 .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 () を呼ぶことで、 再現することが可能である。 .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 を種とする。 .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 () の実装例として以下を挙げている。これは、異なる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) { next = seed; } .fi .in .\"O .SH "SEE ALSO" .SH 関連項目 .BR drand48 (3), .BR random (3)