randのヘルプ・マニュアル
日本語 英語
rand --help
man rand
RAND(3) Linux Programmer’s Manual RAND(3)
名前
rand, srand - 乱数を生成する関数
書式
#include
int rand(void);
int rand_r(unsigned int *seedp);
void srand(unsigned int seed);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
rand_r(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
説明
rand() 関数は [0, RAND_MAX] の範囲の疑似乱数整数を返す。
srand() 関数は、 rand() 関数で作られる疑似乱数整数系列の新しい種として
、その引き数の値を使用する。これらの関数を使用して作られた疑似乱数系 列
は、同じ値を引き数として srand() を呼ぶことで、再現することが可能である
。
種の値が与えられない場合には、 rand() 関数は自動的に 1 を種とする。
rand() 関数は再入可能 (reentrant) ではない、つまりスレッド・セーフで は
な い。なぜなら、この関数は隠し状態を持っており、呼び出される度に変更す
るからである。ちょうどこの隠し状態が次の呼び出し時の乱数の種として使 わ
れ るようなものである。実際にはもう少し複雑かもしれないが。スレッドを使
用するアプリケーションで再現可能な動作をさせたい場合には、この隠し状 態
が 原因で再現性がなくなる。 rand_r() 関数は unsigned int 型の変数へのポ
インタを引き数にとり、この変数を状態格納用に使用する。状態数が非常に 小
さ いので、この関数はあまりよい擬似乱数生成器とは言えないだろう。代わり
に drand48_r(3) を使うとよい。
返り値
rand() と rand_r() 関数は 0 と RAND_MAX の間の数を返す。 srand() 関数は
値を返さない。
準拠
関数 rand() と srand() は SVr4, 4.3BSD, C89, C99, POSIX.1-2001 に準拠し
ている。関数 rand_r() は POSIX.1-2001 に由来する。 POSIX.1-2008 は 、
rand_r() を廃止予定としている。
注意
rand() と srand() の Linux C Library 版は、 random(3) と srandom(3) の
両関数と同じ乱数生成アルゴリズムを使用している。そのため、下位のビッ ト
は 上位のビットと同じくらいにランダムである。しかし、旧版の rand() の実
装や、他のシステムの現在の実装では、下位のビットが上位のビットほどラ ン
ダ ムになっていない。移植性を高める場合でも、精度の高い乱数が必要なアプ
リケーションではこの関数は使用してはいけない (代わりに random(3) を使う
こと)。
例
POSIX 1003.1-2003 では、 rand() と srand() の実装例として以下を挙げてい
る。これは、異なる2つのマシンで同じ乱数系列が必要な場合には便利であ ろ
う。
static unsigned long next = 1;
/* RAND_MAX を 32767 と仮定 */
int myrand(void) {
next = next * 1103515245 + 12345;
return((unsigned)(next/65536) % 32768);
}
void mysrand(unsigned seed) {
next = seed;
}
関連項目
drand48(3), random(3)
2008-08-29 RAND(3)
RAND(3) Linux Programmer’s Manual RAND(3)
NAME
rand, rand_r, srand - pseudo-random number generator
SYNOPSIS
#include
int rand(void);
int rand_r(unsigned int *seedp);
void srand(unsigned int seed);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
rand_r(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
DESCRIPTION
The rand() function returns a pseudo-random integer in the range
[0, RAND_MAX].
The srand() function sets its argument as the seed for a new sequence
of pseudo-random integers to be returned by rand(). These sequences
are repeatable by calling srand() with the same seed value.
If no seed value is provided, the rand() function is automatically
seeded with a value of 1.
The function rand() is not reentrant or thread-safe, since it uses hid-
den state that is modified on each call. This might just be the seed
value to be used by the next call, or it might be something more elabo-
rate. In order to get reproducible behavior in a threaded application,
this state must be made explicit. The function rand_r() is supplied
with a pointer to an unsigned int, to be used as state. This is a very
small amount of state, so this function will be a weak pseudo-random
generator. Try drand48_r(3) instead.
RETURN VALUE
The rand() and rand_r() functions return a value between 0 and
RAND_MAX. The srand() function returns no value.
CONFORMING TO
The functions rand() and srand() conform to SVr4, 4.3BSD, C89, C99,
POSIX.1-2001. The function rand_r() is from POSIX.1-2001.
POSIX.1-2008 marks rand_r() as obsolete.
NOTES
The versions of rand() and srand() in the Linux C Library use the same
random number generator as random(3) and srandom(3), so the lower-order
bits should be as random as the higher-order bits. However, on older
rand() implementations, and on current implementations on different
systems, the lower-order bits are much less random than the higher-
order bits. Do not use this function in applications intended to be
portable when good randomness is needed. (Use random(3) instead.)
EXAMPLE
POSIX.1-2001 gives the following example of an implementation of rand()
and srand(), possibly useful when one needs the same sequence on two
different machines.
static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
int myrand(void) {
next = next * 1103515245 + 12345;
return((unsigned)(next/65536) % 32768);
}
void mysrand(unsigned seed) {
next = seed;
}
SEE ALSO
drand48(3), random(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/.
2008-08-29 RAND(3)