Scroll to navigation

RANDOM_R(3) Linux Programmer's Manual RANDOM_R(3)

名前

random_r, srandom_r, initstate_r, setstate_r - リエントラントな 乱数生成器

書式

#include <stdlib.h>
int random_r(struct random_data *buf, int32_t *result);
int srandom_r(unsigned int seed, struct random_data *buf);
int initstate_r(unsigned int seed, char *statebuf,
                size_t statelen, struct random_data *buf);
int setstate_r(char *statebuf, struct random_data *buf);

glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):

random_r(), srandom_r(), initstate_r(), setstate_r():

/* glibc 2.19 以降: */ _DEFAULT_SOURCE
|| /* glibc 2.19 以前: */ _SVID_SOURCE || _BSD_SOURCE

説明

これらの関数は、 random(3) で説明されている関数のリエントラント版で、等価な機能を持つ。 これらは、独立した再現可能な乱数列を各スレッドが得る必要があるような マルチスレッドプログラムでの使用に適している。

The random_r() function is like random(3), except that instead of using state information maintained in a global variable, it uses the state information in the argument pointed to by buf, which must have been previously initialized by initstate_r(). The generated random number is returned in the argument result.

The srandom_r() function is like srandom(3), except that it initializes the seed for the random number generator whose state is maintained in the object pointed to by buf, which must have been previously initialized by initstate_r(), instead of the seed associated with the global state variable.

The initstate_r() function is like initstate(3) except that it initializes the state in the object pointed to by buf, rather than initializing the global state variable. Before calling this function, the buf.state field must be initialized to NULL. The initstate_r() function records a pointer to the statebuf argument inside the structure pointed to by buf. Thus, statebuf should not be deallocated so long as buf is still in use. (So, statebuf should typically be allocated as a static variable, or allocated on the heap using malloc(3) or similar.)

The setstate_r() function is like setstate(3) except that it modifies the state in the object pointed to by buf, rather than modifying the global state variable. state must first have been initialized using initstate_r() or be the result of a previous call of setstate_r().

返り値

これらの関数はすべて、成功すると 0 を返す。 エラーの場合、-1 を返し、 errno にエラーの原因を示す値がセットされる。

エラー

8 バイトより小さい状態配列が initstate_r() に指定された。
setstate_r() の引数 statebufbuf が NULL であった。
random_r() の引数 bufresult が NULL であった。

属性

この節で使用されている用語の説明については、 attributes(7) を参照。

インターフェース 属性
random_r(), srandom_r(), initstate_r(), setstate_r() Thread safety MT-Safe race:buf

準拠

これらの関数は非標準で glibc による拡張である。

バグ

The initstate_r() interface is confusing. It appears that the random_data type is intended to be opaque, but the implementation requires the user to either initialize the buf.state field to NULL or zero out the entire structure before the call.

関連項目

drand48(3), rand(3), random(3)

この文書について

この man ページは Linux man-pages プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は https://www.kernel.org/doc/man-pages/ に書かれている。

2017-09-15 GNU