OSDN Git Service

(split) Import translated manuals from JM CVS Repository.
[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  2009-02-03 "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 _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
58 .ad b
59 .SH DESCRIPTION
60 The
61 .BR random ()
62 function uses a nonlinear additive feedback random
63 number generator employing a default table of size 31 long integers to
64 return successive pseudo-random numbers in
65 the range from 0 to \fBRAND_MAX\fR.
66 The period of this random number generator is very large, approximately
67 .IR "16\ *\ ((2^31)\ \-\ 1)" .
68 .PP
69 The
70 .BR srandom ()
71 function sets its argument as the seed for a new
72 sequence of pseudo-random integers to be returned by
73 .BR random ().
74 These sequences are repeatable by calling
75 .BR srandom ()
76 with the same
77 seed value.
78 If no seed value is provided, the
79 .BR random ()
80 function
81 is automatically seeded with a value of 1.
82 .PP
83 The
84 .BR initstate ()
85 function allows a state array \fIstate\fP to
86 be initialized for use by
87 .BR random ().
88 The size of the state array
89 \fIn\fP is used by
90 .BR initstate ()
91 to decide how sophisticated a
92 random number generator it should use \(em the larger the state array,
93 the better the random numbers will be.
94 \fIseed\fP is the seed for the
95 initialization, which specifies a starting point for the random number
96 sequence, and provides for restarting at the same point.
97 .PP
98 The
99 .BR setstate ()
100 function changes the state array used by the
101 .BR random ()
102 function.
103 The state array \fIstate\fP is used for
104 random number generation until the next call to
105 .BR initstate ()
106 or
107 .BR setstate ().
108 \fIstate\fP must first have been initialized
109 using
110 .BR initstate ()
111 or be the result of a previous call of
112 .BR setstate ().
113 .SH "RETURN VALUE"
114 The
115 .BR random ()
116 function returns a value between 0 and
117 .BR RAND_MAX .
118 The
119 .BR srandom ()
120 function returns no value.
121 The
122 .BR initstate ()
123 and
124 .BR setstate ()
125 functions return a pointer to the previous state
126 array, or NULL on error.
127 .SH ERRORS
128 .TP
129 .B EINVAL
130 A state array of less than 8 bytes was specified to
131 .BR initstate ().
132 .SH "CONFORMING TO"
133 4.3BSD, POSIX.1-2001.
134 .SH NOTES
135 Current "optimal" values for the size of the state array \fIn\fP are
136 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
137 the nearest known amount.
138 Using less than 8 bytes will cause an
139 error.
140 .PP
141 This function should not be used in cases where multiple threads use
142 .BR random ()
143 and the behavior should be reproducible.
144 Use
145 .BR random_r (3)
146 for that purpose.
147 .PP
148 Random-number generation is a complex topic.
149 .I Numerical Recipes in C: The Art of Scientific Computing
150 (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William
151 T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.)
152 provides an excellent discussion of practical random-number generation
153 issues in Chapter 7 (Random Numbers).
154 .PP
155 For a more theoretical discussion which also covers many practical issues
156 in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's
157 .IR "The Art of Computer Programming" ,
158 volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
159 Addison-Wesley Publishing Company, 1981.
160 .SH "SEE ALSO"
161 .BR drand48 (3),
162 .BR rand (3),
163 .BR random_r (3),
164 .BR srand (3)