.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" References consulted: .\" Linux libc source code .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) .\" 386BSD man pages .\" .\" Modified 1993-03-29, David Metcalfe .\" Modified 1993-04-28, Lars Wirzenius .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu) .\" Modified 1995-05-18, Rik Faith (faith@cs.unc.edu) to add .\" better discussion of problems with rand on other systems. .\" (Thanks to Esa Hyyti{ (ehyytia@snakemail.hut.fi).) .\" Modified 1998-04-10, Nicolás Lichtmaier .\" with contribution from Francesco Potorti .\" Modified 2003-11-15, aeb, added rand_r .\" 2010-09-13, mtk, added example program .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi all rights reserved. .\" Translated 1997-01-21, YOSHINO Takashi .\" Updated 2000-09-24, Kentaro Shirakata .\" Updated 2005-03-15, Akihiro MOTOKI .\" Updated 2008-08-08, Akihiro MOTOKI , LDP v3.05 .\" .TH RAND 3 2014\-01\-18 "" "Linux Programmer's Manual" .SH 名前 rand, srand \- 乱数を生成する関数 .SH 書式 .nf \fB#include \fP .sp \fBint rand(void);\fP .sp \fBint rand_r(unsigned int *\fP\fIseedp\fP\fB);\fP .sp \fBvoid srand(unsigned int \fP\fIseed\fP\fB);\fP .fi .sp .in -4n glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .in .sp \fBrand_r\fP(): _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE .SH 説明 \fBrand\fP() 関数は 0 以上 \fBRAND_MAX\fP 以下 (数学的に書くと [0,\ \fBRAND_MAX\fP]) の範囲の疑似乱数整数を返す。 .PP \fBsrand\fP() 関数は、 \fBrand\fP() 関数で作られる疑似乱数整数系列の新しい種として、 その引き数の値を使用する。 これらの関数を使用して作られた疑似乱数系列は、 同じ値を引き数として \fBsrand\fP() を呼ぶことで、 再現することが可能である。 .PP 種の値が与えられない場合には、 \fBrand\fP() 関数は 自動的に 1 を種とする。 .PP \fBrand\fP() 関数は再入可能 (reentrant) ではない、つまりスレッドセーフではない。 この関数には隠し状態があり、呼び出される度にこの隠し状態が変更されるからである。 ちょうどこの隠し状態が次の呼び出し時の乱数の種として使われるようなものである。 実際にはもう少し複雑かもしれないが。 スレッドを使用するアプリケーションで再現可能な動作をさせたい場合には、 この状態を明示的に指定できなければならない。これを行うには、 再入可能な \fBrand_r\fP() 関数を使用する。 \fBrand\fP() と同様、 \fBrand_r\fP() は [0,\ \fBRAND_MAX\fP] の範囲の疑似乱数整数を返す。 \fIseedp\fP 引き数は、\fBrand_r\fP() の呼び出し間で状態を保持するために使用される \fIunsigned int\fP へのポインターである。 \fIseedp\fP が指す整数に同じ初期値を \fBrand_r\fP() を呼び出し、 呼び出し間でその値が変更されなければ、同じ疑似乱数系列が得られる。 \fBrand_r\fP() の \fIseedp\fP 引き数が指す値により提供される状態は非常に小さな空間 なので、この関数は弱い疑似乱数生成器になってしまう。 代わりに \fBdrand48_r\fP(3) を使ってみるとよい。 .SH 返り値 関数 \fBrand\fP() と \fBrand_r\fP() は 0 以上 \fBRAND_MAX\fP 以下の数を返す。 関数 \fBsrand\fP() は値を返さない。 .SH 準拠 関数 \fBrand\fP() と \fBsrand\fP() は SVr4, 4.3BSD, C89, C99, POSIX.1\-2001 に準拠している。 関数 \fBrand_r\fP() は POSIX.1\-2001 に由来する。 POSIX.1\-2008 は、 \fBrand_r\fP() を廃止予定としている。 .SH 注意 \fBrand\fP() と \fBsrand\fP() の Linux C Library 版は、 \fBrandom\fP(3) と \fBsrandom\fP(3) の両関数と同じ乱数生成 アルゴリズムを使用している。そのため、下位のビットは上位のビットと 同じくらいにランダムである。 しかし、旧版の \fBrand\fP() の実装や、他のシステムの現在の実装では、下位のビットが上位のビットほど ランダムになっていない。移植性を高める場合でも、精度の高い乱数が必要な アプリケーションではこの関数は使用してはいけない (代わりに \fBrandom\fP(3) を使うこと)。 .SH 例 POSIX 1003.1\-2003 では、 \fBrand\fP() と \fBsrand\fP() の実装例として以下を挙げている。これは、異なる2つのマシンで同じ乱数系列が 必要な場合には便利であろう。 .sp .in +4n .nf static unsigned long next = 1; /* RAND_MAX を 32767 と仮定 */ int myrand(void) { next = next * 1103515245 + 12345; return((unsigned)(next/65536) % 32768); } void mysrand(unsigned int seed) { next = seed; } .fi .in .PP 以下のプログラムを使うと、特定の乱数の種が与えられた場合に \fBrand\fP() が生成する疑似乱数系列を表示することができる。 .in +4n .nf #include #include int main(int argc, char *argv[]) { int j, r, nloops; unsigned int seed; if (argc != 3) { fprintf(stderr, "Usage: %s \en", argv[0]); exit(EXIT_FAILURE); } seed = atoi(argv[1]); nloops = atoi(argv[2]); srand(seed); for (j = 0; j < nloops; j++) { r = rand(); printf("%d\en", r); } exit(EXIT_SUCCESS); } .fi .in .SH 関連項目 \fBdrand48\fP(3), \fBrandom\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。