1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\" Modified 1993-03-29, David Metcalfe
29 .\" Modified 1993-04-28, Lars Wirzenius
30 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
31 .\" Modified 1995-05-18, Rik Faith (faith@cs.unc.edu) to add
32 .\" better discussion of problems with rand on other systems.
33 .\" (Thanks to Esa Hyyti{ (ehyytia@snakemail.hut.fi).)
34 .\" Modified 1998-04-10, Nicola'O^ Lichtmaier <nick@debian.org>
35 .\" with contribution from Francesco Potorti <F.Potorti@cnuce.cnr.it>
36 .\" Modified 2003-11-15, aeb, added rand_r
38 .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi all rights reserved.
39 .\" Translated 1997-01-21, YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
40 .\" Updated 2000-09-24, Kentaro Shirakata <argrath@ub32.org>
41 .\" Updated 2005-03-15, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
42 .\" Updated 2008-08-08, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.05
44 .TH RAND 3 2008-08-29 "" "Linux Programmer's Manual"
46 rand, srand \- 乱数を生成する関数
49 .B #include <stdlib.h>
53 .BI "int rand_r(unsigned int *" seedp );
55 .BI "void srand(unsigned int " seed );
60 .RB ( feature_test_macros (7)
65 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
68 関数は [0,\ \fBRAND_MAX\fR] の範囲の疑似乱数整数を返す。
73 関数で作られる疑似乱数整数系列の新しい種として、
75 これらの関数を使用して作られた疑似乱数系列は、
87 関数は再入可能 (reentrant) ではない、つまりスレッド・セーフではない。
88 なぜなら、この関数は隠し状態を持っており、呼び出される度に変更するからである。
89 ちょうどこの隠し状態が次の呼び出し時の乱数の種として使われるようなものである。
90 実際にはもう少し複雑かもしれないが。スレッドを使用するアプリケーションで
91 再現可能な動作をさせたい場合には、この隠し状態が原因で再現性がなくなる。
97 状態数が非常に小さいので、この関数はあまりよい擬似乱数生成器とは言えないだろう。
115 は SVr4, 4.3BSD, C89, C99, POSIX.1-2001 に準拠している。
118 は POSIX.1-2001 に由来する。
126 の Linux C Library 版は、
131 アルゴリズムを使用している。そのため、下位のビットは上位のビットと
135 の実装や、他のシステムの現在の実装では、下位のビットが上位のビットほど
136 ランダムになっていない。移植性を高める場合でも、精度の高い乱数が必要な
137 アプリケーションではこの関数は使用してはいけない
142 POSIX 1003.1-2003 では、
146 の実装例として以下を挙げている。これは、異なる2つのマシンで同じ乱数系列が
151 static unsigned long next = 1;
153 /* RAND_MAX を 32767 と仮定 */
155 next = next * 1103515245 + 12345;
156 return((unsigned)(next/65536) % 32768);
159 void mysrand(unsigned seed) {