.\" 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. .\" .\" 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. .\" %%%LICENSE_END .\" .\" 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 .\" .\"******************************************************************* .\" .\" 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 .\" Updated 2008-08-08, Akihiro MOTOKI .\" Updated 2013-07-24, Akihiro MOTOKI .\" .TH RANDOM 3 2015\-02\-01 GNU "Linux Programmer's Manual" .SH 名前 random, srandom, initstate, setstate \- 乱数を生成する関数 .SH 書式 .nf \fB#include \fP .sp \fBlong int random(void);\fP \fBvoid srandom(unsigned int \fP\fIseed\fP\fB);\fP \fBchar *initstate(unsigned int \fP\fIseed\fP\fB, char *\fP\fIstate\fP\fB, size_t \fP\fIn\fP\fB);\fP .br \fBchar *setstate(char *\fP\fIstate\fP\fB);\fP .fi .sp .in -4n glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .in .sp .ad l \fBrandom\fP(), \fBsrandom\fP(), \fBinitstate\fP(), \fBsetstate\fP(): .RS 4 _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED .RE .ad .SH 説明 \fBrandom\fP() 関数は、非線形加法フィードバックを用いた乱数生成関数である。 この関数は、0 から \fBRAND_MAX\fP までの疑似乱数を返す。 そのために 31 個のロング整数からなるデフォルトの表を使用する。 この乱数を生成する関数の周期はとても長く、およそ \fI16\ *\ ((2^31)\ \-\ 1)\fP である。 .PP \fBsrandom\fP() 関数は、 \fBrandom\fP() で返される疑似乱数整数系列の種を設定する。 そのためには新しい種を引数にして \fBsrandom\fP() を呼べばよい。 \fBrandom\fP() で生成される系列は、 引数に同じ種の値を用いて \fBsrandom\fP() を呼ぶことで再現可能である。 種の値が与えられない場合には \fBrandom\fP() 関数は、自動的に 1 を種に設定する。 .PP \fBinitstate\fP() 関数は、 \fBrandom\fP() で使用される状態配列 \fIstate\fP を初期化する。 \fBinitstate\fP() では、状態配列の大きさ \fIn\fP は 使用する関数の乱数生成の性能の程度を決定するために使用される \(em 状態配列が大きい程、乱数の性能はよくなる。 \fIseed\fP は初期化のための種である。 これは乱数系列の開始位置を決定するものであり、 この値を指定することで同一の開始位置から乱数の生成を 再開することができる。 .PP \fBsetstate\fP() 関数は、 \fBrandom\fP() で使用される状態配列を変更する。 状態配列 \fIstate\fP は、 \fBinitstate\fP() または \fBsetstate\fP() が 次に呼び出されるまで、乱数の生成に使用される。 \fIstate\fP は \fBinitstate\fP() を用いて最初に初期化されているか、 以前に呼び出した \fBsetstate\fP() の結果でなければならない。 .SH 返り値 関数 \fBrandom\fP() と \fBrand_r\fP() は 0 以上 \fBRAND_MAX\fP 以下の数を返す。 関数 \fBsrandom\fP() は値を返さない。 関数 \fBinitstate\fP() は、変更前の状態配列へのポインターを返す。エラーの場合、 \fIerrno\fP に原因を示す値が設定される。 関数 \fBsetstate\fP() は、成功すると 0 を返す。 エラーの場合、\-1 を返し、 \fIerrno\fP にエラーの原因を示す値が設定される。 .SH エラー .TP \fBEINVAL\fP \fBsetstate\fP() の引き数 \fIstate\fP が NULL であった。 .TP \fBEINVAL\fP \fBinitstate\fP() で8バイトよりも小さい状態配列を指定した。 .SH 属性 .SS "マルチスレッディング (pthreads(7) 参照)" 関数 \fBrandom\fP(), \fBsrandom\fP(), \fBinitstate\fP(), \fBsetstate\fP() はスレッドセーフである。 .SH 準拠 4.3BSD, POSIX.1\-2001. .SH 注意 状態配列 \fIn\fP の大きさの現在の「最適」値は 8、32、64、128、256 バイトである。 その他の量を指定した場合には、指定した量を越えない上述の値に 最も近い値になる。 8 バイト未満の量を指定した場合にはエラーの原因となる。 .PP 複数のスレッドが \fBrandom\fP() を使うような状況では、この関数を使用すべきではない。 その場合には \fBrandom_r\fP(3) を使うこと。 .PP 乱数の生成は複雑な話題である。 \fINumerical Recipes in C: The Art of Scientific Computing\fP (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.) では実用的な乱数生成を論点とした優れた議論が第 7 章 (乱数) で展開されている。 .PP より理論的な議論については Donald E. Knuth の \fIThe Art of Computer Programming\fP, volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts: Addison\-Wesley Publishing Company, 1981 の第 3 章 (乱数) を見よ。ここでは、 たくさんの実用的な話題についても深く網羅されている。 .SH バグ .\" http://sourceware.org/bugzilla/show_bug.cgi?id=15380 POSIX では、エラー時に \fBinitstate\fP() は NULL を返すべきとされている。 glibc の実装では、エラー時に \fIerrno\fP は仕様通りに設定されるが、関数の返り値は NULL ではない。 .SH 関連項目 \fBdrand48\fP(3), \fBgetrandom\fP(2), \fBrand\fP(3), \fBrandom_r\fP(3), \fBsrand\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。