summaryrefslogtreecommitdiffstats
path: root/man3/random.3
blob: d7940f4aa7294f5a0de6d9d57d89468e938beffd (plain) (blame)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
.\" 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 Sun Mar 28 00:25:51 1993, David Metcalfe
.\" Modified Sat Jul 24 18:13:39 1993 by Rik Faith (faith@cs.unc.edu)
.\" Modified Sun Aug 20 21:47:07 2000, aeb
.\"
.TH RANDOM 3  2020-11-01 "GNU" "Linux Programmer's Manual"
.SH NAME
random, srandom, initstate, setstate \- random number generator
.SH SYNOPSIS
.nf
.B #include <stdlib.h>
.PP
.B long random(void);
.PP
.BI "void srandom(unsigned " seed );
.PP
.BI "char *initstate(unsigned " seed ", char *" state ", size_t " n );
.PP
.BI "char *setstate(char *" state );
.fi
.PP
.RS -4
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.RE
.PP
.ad l
.BR random (),
.BR srandom (),
.BR initstate (),
.BR setstate ():
.RS 4
_XOPEN_SOURCE\ >=\ 500
.\"    || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
    || /* Glibc since 2.19: */ _DEFAULT_SOURCE
    || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
.RE
.ad
.SH DESCRIPTION
The
.BR random ()
function uses a nonlinear additive feedback random
number generator employing a default table of size 31 long integers to
return successive pseudo-random numbers in
the range from 0 to 2^31\ \-\ 1.
The period of this random number generator is very large, approximately
.IR "16\ *\ ((2^31)\ \-\ 1)" .
.PP
The
.BR srandom ()
function sets its argument as the seed for a new
sequence of pseudo-random integers to be returned by
.BR random ().
These sequences are repeatable by calling
.BR srandom ()
with the same
seed value.
If no seed value is provided, the
.BR random ()
function
is automatically seeded with a value of 1.
.PP
The
.BR initstate ()
function allows a state array \fIstate\fP to
be initialized for use by
.BR random ().
The size of the state array
\fIn\fP is used by
.BR initstate ()
to decide how sophisticated a
random number generator it should use\(emthe larger the state array,
the better the random numbers will be.
Current "optimal" values for the size of the state array \fIn\fP are
8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
the nearest known amount.
Using less than 8 bytes results in an error.
\fIseed\fP is the seed for the
initialization, which specifies a starting point for the random number
sequence, and provides for restarting at the same point.
.PP
The
.BR setstate ()
function changes the state array used by the
.BR random ()
function.
The state array \fIstate\fP is used for
random number generation until the next call to
.BR initstate ()
or
.BR setstate ().
\fIstate\fP must first have been initialized
using
.BR initstate ()
or be the result of a previous call of
.BR setstate ().
.SH RETURN VALUE
The
.BR random ()
function returns a value between 0 and
.IR "(2^31)\ \-\ 1" .
The
.BR srandom ()
function returns no value.
.PP
The
.BR initstate ()
function returns a pointer to the previous state array.
On error,
.I errno
is set to indicate the cause.
.PP
On success,
.BR setstate ()
returns a pointer to the previous state array.
On error, it returns NULL, with
.I errno
set to indicate the cause of the error.
.SH ERRORS
.TP
.B EINVAL
The
.I state
argument given to
.BR setstate ()
was NULL.
.TP
.B EINVAL
A state array of less than 8 bytes was specified to
.BR initstate ().
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lbw23 lb lb
l l l.
Interface	Attribute	Value
T{
.BR random (),
.BR srandom (),
.br
.BR initstate (),
.BR setstate ()
T}	Thread safety	MT-Safe
.TE
.SH CONFORMING TO
POSIX.1-2001, POSIX.1-2008, 4.3BSD.
.SH NOTES
The
.BR random ()
function should not be used in multithreaded programs
where reproducible behavior is required.
Use
.BR random_r (3)
for that purpose.
.PP
Random-number generation is a complex topic.
.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, 2007, 3rd ed.)
provides an excellent discussion of practical random-number generation
issues in Chapter 7 (Random Numbers).
.PP
For a more theoretical discussion which also covers many practical issues
in depth, see Chapter 3 (Random Numbers) in Donald E.\& Knuth's
.IR "The Art of Computer Programming" ,
volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
Addison-Wesley Publishing Company, 1981.
.SH BUGS
According to POSIX,
.BR initstate ()
should return NULL on error.
In the glibc implementation,
.I errno
is (as specified) set on error, but the function does not return NULL.
.\" http://sourceware.org/bugzilla/show_bug.cgi?id=15380
.SH SEE ALSO
.BR getrandom (2),
.BR drand48 (3),
.BR rand (3),
.BR random_r (3),
.BR srand (3)
.SH COLOPHON
This page is part of release 5.09 of the Linux
.I man-pages
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
\%https://www.kernel.org/doc/man\-pages/.