1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
.\"
.\" 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.
.\"
.\" 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, Nicola'O^ Lichtmaier <nick@debian.org>
.\" with contribution from Francesco Potorti <F.Potorti@cnuce.cnr.it>
.\" Modified 2003-11-15, aeb, added rand_r
.\"
.\" Japanese Version Copyright (c) 1997 YOSHINO Takashi all rights reserved.
.\" Translated 1997-01-21, YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
.\" Updated 2000-09-24, Kentaro Shirakata <argrath@ub32.org>
.\" Updated 2005-03-15, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
.\"
.TH RAND 3 2007-07-26 "" "Linux Programmer's Manual"
.SH ̾
rand, srand \- ؿ
.SH
.nf
.B #include <stdlib.h>
.sp
.B int rand(void);
.sp
.BI "int rand_r(unsigned int *" seedp );
.sp
.BI "void srand(unsigned int " seed );
.fi
.sp
.in -4n
glibc εǽޥ
.RB ( feature_test_macros (7)
):
.in
.sp
.BR rand_r ():
_POSIX_C_SOURCE || _XOPEN_SOURCE
.SH
.BR rand ()
ؿ 0 \fBRAND_MAX\fR δ֤ε֤
.PP
.BR srand ()
ؿϡ
.BR rand ()
ؿǺ뵿οȤơ
ΰͤѤ롣
δؿѤƺ줿ϡ
ƱͤȤ
.BR srand ()
Ƥ֤Ȥǡ
Ƹ뤳ȤǽǤ롣
.PP
ͤͿʤˤϡ
.BR rand ()
ؿ
ưŪ 1 Ȥ롣
.PP
.BR rand ()
ؿϺǽ (reentrant) ǤϤʤĤޤꥹåɡդǤϤʤ
ʤʤ顢δؿϱ֤äƤꡢƤӽФ٤ѹ뤫Ǥ롣
礦ɤα֤θƤӽФμȤƻȤ褦ʤΤǤ롣
ºݤˤϤ⤦ʣ⤷ʤåɤѤ륢ץꥱ
Ƹǽưˤϡα֤ǺƸʤʤ롣
.BR rand_r ()
ؿ
.I unsigned int
ѿؤΥݥˤȤꡢ
ѿֳǼѤ˻Ѥ롣
ֿ˾ΤǡδؿϤޤ褤Ȥϸʤ
.BR drand48_r (3)
ȤȤ褤
.SH ֤
.BR rand ()
.BR rand_r ()
ؿ 0
.B RAND_MAX
δ֤ο֤
.BR srand ()
ؿ֤ͤʤ
.SH
ؿ
.BR rand ()
.BR srand ()
SVr4, 4.3BSD, C89, C99, POSIX.1-2001 ˽Ƥ롣
ؿ
.BR rand_r ()
POSIX.1-2001 ͳ褹롣
.SH
.BR rand ()
.BR srand ()
Linux C Library Ǥϡ
.BR random (3)
.BR srandom (3)
ξؿƱ
르ꥺѤƤ롣Τᡢ̤ΥӥåȤϾ̤ΥӥåȤ
Ʊ餤˥Ǥ롣
Ǥ
.BR rand ()
μ䡢¾ΥƥθߤμǤϡ̤ΥӥåȤ̤ΥӥåȤۤ
ˤʤäƤʤܿǤ⡢٤ι⤤ɬפ
ץꥱǤϤδؿϻѤƤϤʤ
.PP
.I 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, 1992 (2nd ed.,
p. 277))
ǤϡΤ褦ʥȤʤƤ롣
.PP
.RS 4
1 10 δ֤ΤǤС
.in +4n
.sp
j = 1 + (int) (10.0 * rand() / (RAND_MAX + 1.0));
.sp
.in
Τ褦˾̥ӥåȤѤ뤳ȡ
.in +4n
.sp
j = 1 + (rand() % 10);
.sp
.in
Τ褦ʡ̤ΥӥåȤѤ褦ˡϷ褷Ƥʤȡ
.RE
.PP
ʣǤ롣Ҥ
.I Numerical Recipes in C
ǤϼŪȤͥ줿 7 () ŸƤ롣
.PP
ŪʵˤĤƤ Donald E. Knuth
.IR "The Art of Computer Programming" ,
volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
Addison-Wesley Publishing Company, 1981
3 () 衣Ǥϡ
μŪˤĤƤ夵Ƥ롣
.SH
POSIX 1003.1-2003 Ǥϡ
.BR rand ()
.BR srand ()
μȤưʲƤ롣ϡۤʤ룲ĤΥޥƱ
ɬפʾˤǤ
.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 seed) {
next = seed;
}
.fi
.in
.SH Ϣ
.BR drand48 (3),
.BR random (3)
|