.\" 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 Sun Mar 28 00:25:51 1993, David Metcalfe .\" Modified Sat Jul 24 18:13:39 1993 by Rik Faith (faith@cs.unc.edu) .\" Modified Sun Aug 20 21:47:07 2000, aeb .\" .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi .\" all rights reserved. .\" Translated 1997-01-21, YOSHINO Takashi .\" Updated 2008-08-08, Akihiro MOTOKI .\" .\" WORD: nonlinear additive feedback 非線形加法フィードバック .\" WORD: state array 状態配列 .\" .TH RANDOM 3 2010-09-20 "GNU" "Linux Programmer's Manual" .\"O .SH NAME .SH 名前 .\"O random, srandom, initstate, setstate \- random number generator random, srandom, initstate, setstate \- 乱数を生成する関数 .\"O .SH SYNOPSIS .SH 書式 .nf .B #include .sp .B long int random(void); .br .BI "void srandom(unsigned int " seed ); .BI "char *initstate(unsigned int " seed ", char *" state ", size_t " n ); .BI "char *setstate(char *" state ); .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 .ad l .BR random (), .BR srandom (), .BR initstate (), .BR setstate (): .RS 4 _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED .RE .ad .\"O .SH DESCRIPTION .SH 説明 .\"O The .\"O .BR random () .\"O function uses a nonlinear additive feedback random .\"O number generator employing a default table of size 31 long integers to .\"O return successive pseudo-random numbers in .\"O the range from 0 to \fBRAND_MAX\fR. .\"O The period of this random number generator is very large, approximately .\"O .IR "16\ *\ ((2^31)\ \-\ 1)" . .BR random () 関数は、非線形加法フィードバックを用いた乱数生成関数である。 この関数は、0 から \fBRAND_MAX\fR までの疑似乱数を返す。 そのために 31 個のロング整数からなるデフォルトの表を使用する。 この乱数を生成する関数の周期はとても長く、およそ .I "16\ *\ ((2^31)\ \-\ 1)" である。 .PP .\"O The .\"O .BR srandom () .\"O function sets its argument as the seed for a new .\"O sequence of pseudo-random integers to be returned by .\"O .BR random (). .\"O These sequences are repeatable by calling .\"O .BR srandom () .\"O with the same .\"O seed value. .\"O If no seed value is provided, the .\"O .BR random () .\"O function .\"O is automatically seeded with a value of 1. .BR srandom () 関数は、 .BR random () で返される疑似乱数整数系列の種を設定する。 そのためには新しい種を引数にして .BR srandom () を呼べばよい。 .BR random () で生成される系列は、 引数に同じ種の値を用いて .BR srandom () を呼ぶことで再現可能である。 種の値が与えられない場合には .BR random () 関数は、自動的に 1 を種に設定する。 .PP .\"O The .\"O .BR initstate () .\"O function allows a state array \fIstate\fP to .\"O be initialized for use by .\"O .BR random (). .\"O The size of the state array .\"O \fIn\fP is used by .\"O .BR initstate () .\"O to decide how sophisticated a .\"O random number generator it should use \(em the larger the state array, .\"O the better the random numbers will be. .\"O \fIseed\fP is the seed for the .\"O initialization, which specifies a starting point for the random number .\"O sequence, and provides for restarting at the same point. .BR initstate () 関数は、 .BR random () で使用される状態配列 \fIstate\fP を初期化する。 .BR initstate () では、状態配列の大きさ \fIn\fP は 使用する関数の乱数生成の性能の程度を決定するために使用される \(em 状態配列が大きい程、乱数の性能はよくなる。 \fIseed\fP は初期化のための種である。 これは乱数系列の開始位置を決定するものであり、 この値を指定することで同一の開始位置から乱数の生成を 再開することができる。 .PP .\"O The .\"O .BR setstate () .\"O function changes the state array used by the .\"O .BR random () .\"O function. .\"O The state array \fIstate\fP is used for .\"O random number generation until the next call to .\"O .BR initstate () .\"O or .\"O .BR setstate (). .\"O \fIstate\fP must first have been initialized .\"O using .\"O .BR initstate () .\"O or be the result of a previous call of .\"O .BR setstate (). .BR setstate () 関数は、 .BR random () で使用される状態配列を変更する。 状態配列 \fIstate\fP は、 .BR initstate () または .BR setstate () が 次に呼び出されるまで、乱数の生成に使用される。 \fIstate\fP は .BR initstate () を用いて最初に初期化されているか、 以前に呼び出した .BR setstate () の結果でなければならない。 .\"O .SH "RETURN VALUE" .SH 返り値 .\"O The .\"O .BR random () .\"O function returns a value between 0 and .\"O .BR RAND_MAX . .\"O The .\"O .BR srandom () .\"O function returns no value. .\"O The .\"O .BR initstate () .\"O function returns a pointer to the previous state array. .\"O The .\"O .BR setstate () .\"O function returns a pointer to the previous state array, or NULL on error. .BR random () 関数は 0 と .B RAND_MAX の間の値を返す。 .BR srandom () 関数は値を返さない。 .BR initstate () 関数は直前の状態配列へのポインタを返す。 .BR setstate () 関数は直前の状態配列へのポインタを返し、 エラーの場合は NULL を返す。 .\"O .SH ERRORS .SH エラー .TP .B EINVAL .\"O A state array of less than 8 bytes was specified to .\"O .BR initstate (). .BR initstate () で8バイトよりも小さい状態配列を指定した。 .\"O .SH "CONFORMING TO" .\"O 4.3BSD .SH 準拠 4.3BSD, POSIX.1-2001. .\"O .SH NOTES .SH 注意 .\"O Current "optimal" values for the size of the state array \fIn\fP are .\"O 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to .\"O the nearest known amount. .\"O Using less than 8 bytes will cause an .\"O error. 状態配列 \fIn\fP の大きさの現在の「最適」値は 8、32、64、128、256 バイトである。 その他の量を指定した場合には、指定した量を越えない上述の値に 最も近い値になる。 8 バイト未満の量を指定した場合にはエラーの原因となる。 .\"O This function should not be used in cases where multiple threads use .\"O .BR random () .\"O and the behavior should be reproducible. .\"O Use .\"O .BR random_r (3) .\"O for that purpose. 複数のスレッドが .BR random () を使うような状況では、この関数を使用すべきではない。 その場合には .BR random_r (3) を使うこと。 .\"O Random-number generation is a complex topic. .\"O .I Numerical Recipes in C: The Art of Scientific Computing .\"O (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William .\"O T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.) .\"O provides an excellent discussion of practical random-number generation .\"O issues in Chapter 7 (Random Numbers). 乱数の生成は複雑な話題である。 .I Numerical Recipes in C: The Art of Scientific Computing (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.) では実用的な乱数生成を論点とした優れた議論が第 7 章 (乱数) で展開されている。 .\"O For a more theoretical discussion which also covers many practical issues .\"O in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's .\"O .IR "The Art of Computer Programming" , .\"O volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts: .\"O Addison-Wesley Publishing Company, 1981. より理論的な議論については Donald E. Knuth の .IR "The Art of Computer Programming" , volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts: Addison-Wesley Publishing Company, 1981 の第 3 章 (乱数) を見よ。ここでは、 たくさんの実用的な話題についても深く網羅されている。 .\"O .SH "SEE ALSO" .SH 関連項目 .BR drand48 (3), .BR rand (3), .BR random_r (3), .BR srand (3)