#include <stdlib.h>
long random(void);
void srandom(unsigned int seed);
char *initstate(size_t n;
unsigned int seed, char state[n], size_t n);
char *setstate(char *state);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
random(), srandom(), initstate(), setstate():
_XOPEN_SOURCE >= 500
|| /* glibc >= 2.19: */ _DEFAULT_SOURCE
|| /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
srandom() 関数は、 random() で返される疑似乱数整数系列の種を設定する。 そのためには新しい種を引数にして srandom() を呼べばよい。 random() で生成される系列は、 引数に同じ種の値を用いて srandom() を呼ぶことで再現可能である。 種の値が与えられない場合には random() 関数は、自動的に 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 initstate() to decide how sophisticated a random number generator it should use---the larger the state array, the better the random numbers will be. 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 nearest known amount. Using less than 8 bytes results in an error. seed is the seed for the initialization, which specifies a starting point for the random number sequence, and provides for restarting at the same point.
setstate() 関数は、 random() で使用される状態配列を変更する。 状態配列 state は、 initstate() または setstate() が 次に呼び出されるまで、乱数の生成に使用される。 state は initstate() を用いて最初に初期化されているか、 以前に呼び出した setstate() の結果でなければならない。
The initstate() function returns a pointer to the previous state array. On failure, it returns NULL, and errno is set to indicate the error.
On success, setstate() returns a pointer to the previous state array. On failure, it returns NULL, and errno is set to indicate the error.
| インターフェース | 属性 | 値 |
| random(), srandom(), initstate(), setstate() | Thread safety | MT-Safe |
より理論的な議論については Donald E. Knuth の The Art of Computer Programming, volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts: Addison-Wesley Publishing Company, 1981 の第 3 章 (乱数) を見よ。ここでは、 たくさんの実用的な話題についても深く網羅されている。