OSDN Git Service

(split) LDP man-pages の original/ を v3.29 に更新。
[linuxjm/LDP_man-pages.git] / original / man3 / random.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
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.
6 .\"
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.
11 .\"
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
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\"     Linux libc source code
25 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\"     386BSD man pages
27 .\" Modified Sun Mar 28 00:25:51 1993, David Metcalfe
28 .\" Modified Sat Jul 24 18:13:39 1993 by Rik Faith (faith@cs.unc.edu)
29 .\" Modified Sun Aug 20 21:47:07 2000, aeb
30 .\"
31 .TH RANDOM 3  2010-09-20 "GNU" "Linux Programmer's Manual"
32 .SH NAME
33 random, srandom, initstate, setstate \- random number generator
34 .SH SYNOPSIS
35 .nf
36 .B #include <stdlib.h>
37 .sp
38 .B long int random(void);
39
40 .BI "void srandom(unsigned int " seed );
41
42 .BI "char *initstate(unsigned int " seed ", char *" state ", size_t " n );
43 .br
44 .BI "char *setstate(char *" state );
45 .fi
46 .sp
47 .in -4n
48 Feature Test Macro Requirements for glibc (see
49 .BR feature_test_macros (7)):
50 .in
51 .sp
52 .ad l
53 .BR random (),
54 .BR srandom (),
55 .BR initstate (),
56 .BR setstate ():
57 .RS 4
58 _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
59 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
60 .RE
61 .ad
62 .SH DESCRIPTION
63 The
64 .BR random ()
65 function uses a nonlinear additive feedback random
66 number generator employing a default table of size 31 long integers to
67 return successive pseudo-random numbers in
68 the range from 0 to \fBRAND_MAX\fR.
69 The period of this random number generator is very large, approximately
70 .IR "16\ *\ ((2^31)\ \-\ 1)" .
71 .PP
72 The
73 .BR srandom ()
74 function sets its argument as the seed for a new
75 sequence of pseudo-random integers to be returned by
76 .BR random ().
77 These sequences are repeatable by calling
78 .BR srandom ()
79 with the same
80 seed value.
81 If no seed value is provided, the
82 .BR random ()
83 function
84 is automatically seeded with a value of 1.
85 .PP
86 The
87 .BR initstate ()
88 function allows a state array \fIstate\fP to
89 be initialized for use by
90 .BR random ().
91 The size of the state array
92 \fIn\fP is used by
93 .BR initstate ()
94 to decide how sophisticated a
95 random number generator it should use \(em the larger the state array,
96 the better the random numbers will be.
97 \fIseed\fP is the seed for the
98 initialization, which specifies a starting point for the random number
99 sequence, and provides for restarting at the same point.
100 .PP
101 The
102 .BR setstate ()
103 function changes the state array used by the
104 .BR random ()
105 function.
106 The state array \fIstate\fP is used for
107 random number generation until the next call to
108 .BR initstate ()
109 or
110 .BR setstate ().
111 \fIstate\fP must first have been initialized
112 using
113 .BR initstate ()
114 or be the result of a previous call of
115 .BR setstate ().
116 .SH "RETURN VALUE"
117 The
118 .BR random ()
119 function returns a value between 0 and
120 .BR RAND_MAX .
121 The
122 .BR srandom ()
123 function returns no value.
124 The
125 .BR initstate ()
126 function returns a pointer to the previous state array.
127 The
128 .BR setstate ()
129 function returns a pointer to the previous state array, or NULL on error.
130 .SH ERRORS
131 .TP
132 .B EINVAL
133 A state array of less than 8 bytes was specified to
134 .BR initstate ().
135 .SH "CONFORMING TO"
136 4.3BSD, POSIX.1-2001.
137 .SH NOTES
138 Current "optimal" values for the size of the state array \fIn\fP are
139 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
140 the nearest known amount.
141 Using less than 8 bytes will cause an
142 error.
143 .PP
144 This function should not be used in cases where multiple threads use
145 .BR random ()
146 and the behavior should be reproducible.
147 Use
148 .BR random_r (3)
149 for that purpose.
150 .PP
151 Random-number generation is a complex topic.
152 .I Numerical Recipes in C: The Art of Scientific Computing
153 (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William
154 T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.)
155 provides an excellent discussion of practical random-number generation
156 issues in Chapter 7 (Random Numbers).
157 .PP
158 For a more theoretical discussion which also covers many practical issues
159 in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's
160 .IR "The Art of Computer Programming" ,
161 volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
162 Addison-Wesley Publishing Company, 1981.
163 .SH "SEE ALSO"
164 .BR drand48 (3),
165 .BR rand (3),
166 .BR random_r (3),
167 .BR srand (3)