random

Section: C Library Functions (3)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

random, srandom, initstate, setstate - 乱数を生成する関数  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#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
 

説明

The random() function uses a nonlinear 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 2^31 - 1. The period of this random number generator is very large, approximately 16 * ((2^31) - 1).

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 random() function returns a value between 0 and (2^31) - 1. The srandom() function returns no value.

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.  

エラー

EINVAL
setstate() の引数 state が NULL であった。
EINVAL
initstate() で8バイトよりも小さい状態配列を指定した。
 

属性

この節で使用されている用語の説明については、 attributes(7) を参照。
インターフェース属性値
random(), srandom(), initstate(), setstate() Thread safetyMT-Safe
 

標準規格

POSIX.1-2008.  

歴史

POSIX.1-2001, 4.3BSD.  

注意

乱数の生成は複雑な話題である。 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 Programming, volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts: Addison-Wesley Publishing Company, 1981 の第 3 章 (乱数) を見よ。ここでは、 たくさんの実用的な話題についても深く網羅されている。  

警告

The random() function should not be used in multithreaded programs where reproducible behavior is required. Use random_r(3) for that purpose.  

バグ

POSIX では、エラー時に initstate() は NULL を返すべきとされている。 glibc の実装では、エラー時に errno は仕様通りに設定されるが、関数の返り値は NULL ではない。  

関連項目

getrandom(2), drand48(3), rand(3), random_r(3), srand(3)


 

Index

名前
ライブラリ
書式
説明
返り値
エラー
属性
標準規格
歴史
注意
警告
バグ
関連項目

This document was created by man2html, using the manual pages.