randomのヘルプ・マニュアル
日本語 英語
random --help
man random
RANDOM(3) Linux Programmer’s Manual RANDOM(3)
名前
random, srandom, initstate, setstate - 乱数を生成する関数
書式
#include
long int random(void);
void srandom(unsigned int seed);
char *initstate(unsigned int seed, char *state, size_t n);
char *setstate(char *state);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
random(), srandom(), initstate(), setstate(): _SVID_SOURCE ||
_BSD_SOURCE || _XOPEN_SOURCE >= 500
説明
random() 関数は、非線形加法フィードバックを用いた乱数生成関数である。こ
の 関数は、0 から RAND_MAX までの疑似乱数を返す。そのために 31 個のロン
グ整数からなるデフォルトの表を使用する。この乱数を生成する関数の周期 は
とても長く、およそ 16 * ((2^31) - 1) である。
srandom() 関数は、 random() で返される疑似乱数整数系列の種を設定する。
そのためには新しい種を引数にして srandom() を呼べばよい。 random() で生
成される系列は、引数に同じ種の値を用いて srandom() を呼ぶことで再現可能
である。種の値が与えられない場合には random() 関数は、自動的に 1 を種に
設定する。
initstate() 関数は、 random() で使用される状態配列 state を初期化する。
initstate() では、状態配列の大きさ n は使用する関数の乱数生成の性能の程
度を決定するために使用される — 状態配列が大きい程、乱数の性能はよくなる
。 seed は初期化のための種である。これは乱数系列の開始位置を決定する も
の であり、この値を指定することで同一の開始位置から乱数の生成を再開する
ことができる。
setstate() 関数は、 random() で使用される状態配列を変更する。状態 配 列
state は、 initstate() または setstate() が次に呼び出されるまで、乱数の
生成に使用される。 state は initstate() を用いて最初に初期化されてい る
か、以前に呼び出した setstate() の結果でなければならない。
返り値
random() 関数は 0 と RAND_MAX の間の値を返す。 srandom() 関数は値を返さ
ない。 initstate() 関数と setstate() 関数は直前の状態配列へのポインタま
たは NULL を返す。
エラー
EINVAL initstate() で8バイトよりも小さい状態配列を指定した。
準拠
4.3BSD, POSIX.1-2001.
注意
状 態配列 n の大きさの現在の「最適」値は 8、32、64、128、256 バイトであ
る。その他の量を指定した場合には、指定した量を越えない上述の値に最も 近
い値になる。 8 バイト未満の量を指定した場合にはエラーの原因となる。
複 数のスレッドが random() を使うような状況では、この関数を使用すべきで
はない。その場合には random_r(3) を使うこと。
乱数の生成は複雑な話題である。 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 章 (乱
数) で展開されている。
より理論的な議論については Donald E. Knuth の The Art of Computer Pro-
gramming, volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Mas-
sachusetts: Addison-Wesley Publishing Company, 1981 の第 3 章 (乱数) を
見よ。ここでは、たくさんの実用的な話題についても深く網羅されている。
関連項目
drand48(3), rand(3), random_r(3), srand(3)
GNU 2009-02-03 RANDOM(3)
RANDOM(3) Linux Programmer’s Manual RANDOM(3)
NAME
random, srandom, initstate, setstate - random number generator
SYNOPSIS
#include
long int random(void);
void srandom(unsigned int seed);
char *initstate(unsigned int seed, char *state, size_t n);
char *setstate(char *state);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
random(), srandom(), initstate(), setstate(): _SVID_SOURCE ||
_BSD_SOURCE || _XOPEN_SOURCE >= 500
DESCRIPTION
The random() function uses a non-linear additive feedback random number
generator employing a default table of size 31 long integers to return
successive pseudo-random numbers in the range from 0 to RAND_MAX. The
period of this random number generator is very large, approximately
16 * ((2^31) - 1).
The srandom() function sets its argument as the seed for a new sequence
of pseudo-random integers to be returned by random(). These sequences
are repeatable by calling srandom() with the same seed value. If no
seed value is provided, the random() function is automatically seeded
with a value of 1.
The initstate() function allows a state array state to be initialized
for use by random(). The size of the state array n is used by init-
state() to decide how sophisticated a random number generator it should
use — the larger the state array, the better the random numbers will
be. seed is the seed for the initialization, which specifies a start-
ing point for the random number sequence, and provides for restarting
at the same point.
The setstate() function changes the state array used by the random()
function. The state array state is used for random number generation
until the next call to initstate() or setstate(). state must first
have been initialized using initstate() or be the result of a previous
call of setstate().
RETURN VALUE
The random() function returns a value between 0 and RAND_MAX. The
srandom() function returns no value. The initstate() and setstate()
functions return a pointer to the previous state array, or NULL on
error.
ERRORS
EINVAL A state array of less than 8 bytes was specified to initstate().
CONFORMING TO
4.3BSD, POSIX.1-2001.
NOTES
Current "optimal" values for the size of the state array n are 8, 32,
64, 128, and 256 bytes; other amounts will be rounded down to the near-
est known amount. Using less than 8 bytes will cause an error.
This function should not be used in cases where multiple threads use
random() and the behavior should be reproducible. Use random_r(3) for
that purpose.
Random-number generation is a complex topic. Numerical Recipes in C:
The Art of Scientific Computing (William H. Press, Brian P. Flannery,
Saul A. Teukolsky, William T. Vetterling; New York: Cambridge Univer-
sity Press, 2007, 3rd ed.) provides an excellent discussion of practi-
cal random-number generation issues in Chapter 7 (Random Numbers).
For a more theoretical discussion which also covers many practical
issues in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth’s
The Art of Computer Programming, volume 2 (Seminumerical Algorithms),
2nd ed.; Reading, Massachusetts: Addison-Wesley Publishing Company,
1981.
SEE ALSO
drand48(3), rand(3), random_r(3), srand(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2009-02-03 RANDOM(3)