]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/random.3
fanotify_init.2, fanotify.7: Document FAN_REPORT_TID
[thirdparty/man-pages.git] / man3 / random.3
CommitLineData
fea681da
MK
1.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
7.\"
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
c13182ef 12.\"
fea681da
MK
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
16.\" the use of the information contained herein. The author(s) may not
17.\" have taken the same level of care in the production of this manual,
18.\" which is licensed free of charge, as they might when working
19.\" professionally.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
fea681da
MK
24.\"
25.\" References consulted:
26.\" Linux libc source code
27.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28.\" 386BSD man pages
29.\" Modified Sun Mar 28 00:25:51 1993, David Metcalfe
30.\" Modified Sat Jul 24 18:13:39 1993 by Rik Faith (faith@cs.unc.edu)
31.\" Modified Sun Aug 20 21:47:07 2000, aeb
32.\"
4b8c67d9 33.TH RANDOM 3 2017-09-15 "GNU" "Linux Programmer's Manual"
fea681da
MK
34.SH NAME
35random, srandom, initstate, setstate \- random number generator
36.SH SYNOPSIS
37.nf
38.B #include <stdlib.h>
68e4db0a 39.PP
fea681da 40.B long int random(void);
dbfe9c70 41.PP
fea681da 42.BI "void srandom(unsigned int " seed );
dbfe9c70 43.PP
fea681da 44.BI "char *initstate(unsigned int " seed ", char *" state ", size_t " n );
dbfe9c70 45.PP
fea681da
MK
46.BI "char *setstate(char *" state );
47.fi
68e4db0a 48.PP
cc4615cc
MK
49.in -4n
50Feature Test Macro Requirements for glibc (see
51.BR feature_test_macros (7)):
52.in
68e4db0a 53.PP
cc4615cc
MK
54.ad l
55.BR random (),
56.BR srandom (),
57.BR initstate (),
58.BR setstate ():
bb1029be 59.RS 4
2b1b0424 60_XOPEN_SOURCE\ >=\ 500
cf7fa0a1 61.\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
2b1b0424
MK
62 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
63 || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
bb1029be
MK
64.RE
65.ad
fea681da 66.SH DESCRIPTION
60a90ecd
MK
67The
68.BR random ()
fb0e9c48 69function uses a nonlinear additive feedback random
fea681da 70number generator employing a default table of size 31 long integers to
c13182ef
MK
71return successive pseudo-random numbers in
72the range from 0 to \fBRAND_MAX\fR.
fea681da 73The period of this random number generator is very large, approximately
b983ad10 74.IR "16\ *\ ((2^31)\ \-\ 1)" .
fea681da 75.PP
60a90ecd
MK
76The
77.BR srandom ()
78function sets its argument as the seed for a new
79sequence of pseudo-random integers to be returned by
80.BR random ().
81These sequences are repeatable by calling
82.BR srandom ()
83with the same
c13182ef 84seed value.
60a90ecd
MK
85If no seed value is provided, the
86.BR random ()
87function
fea681da
MK
88is automatically seeded with a value of 1.
89.PP
60a90ecd
MK
90The
91.BR initstate ()
92function allows a state array \fIstate\fP to
93be initialized for use by
94.BR random ().
c13182ef 95The size of the state array
60a90ecd
MK
96\fIn\fP is used by
97.BR initstate ()
98to decide how sophisticated a
5503c85e 99random number generator it should use\(emthe larger the state array,
be7fff26 100the better the random numbers will be.
f9a54fa1
MK
101Current "optimal" values for the size of the state array \fIn\fP are
1028, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
103the nearest known amount.
104Using less than 8 bytes results in an error.
be7fff26 105\fIseed\fP is the seed for the
fea681da
MK
106initialization, which specifies a starting point for the random number
107sequence, and provides for restarting at the same point.
108.PP
60a90ecd
MK
109The
110.BR setstate ()
111function changes the state array used by the
112.BR random ()
113function.
c13182ef 114The state array \fIstate\fP is used for
60a90ecd
MK
115random number generation until the next call to
116.BR initstate ()
117or
118.BR setstate ().
119\fIstate\fP must first have been initialized
120using
121.BR initstate ()
122or be the result of a previous call of
123.BR setstate ().
47297adb 124.SH RETURN VALUE
60a90ecd
MK
125The
126.BR random ()
2f0af33b
MK
127function returns a value between 0 and
128.BR RAND_MAX .
60a90ecd
MK
129The
130.BR srandom ()
131function returns no value.
847e0d88 132.PP
60a90ecd
MK
133The
134.BR initstate ()
247cb8f5 135function returns a pointer to the previous state array.
6d1a3ba6
MK
136On error,
137.I errno
138is set to indicate the cause.
847e0d88 139.PP
6d1a3ba6 140On success,
60a90ecd 141.BR setstate ()
6d1a3ba6
MK
142returns a pointer to the previous state array.
143On error, it returns NULL, with
144.I errno
145set to indicate the cause of the error.
fea681da
MK
146.SH ERRORS
147.TP
148.B EINVAL
d2fdbe9c
MK
149The
150.I state
151argument given to
152.BR setstate ()
153was NULL.
154.TP
155.B EINVAL
60a90ecd
MK
156A state array of less than 8 bytes was specified to
157.BR initstate ().
a04d1466 158.SH ATTRIBUTES
52f091ee
PH
159For an explanation of the terms used in this section, see
160.BR attributes (7).
161.TS
162allbox;
163lbw23 lb lb
164l l l.
165Interface Attribute Value
166T{
a04d1466
PH
167.BR random (),
168.BR srandom (),
52f091ee 169.br
a04d1466 170.BR initstate (),
a04d1466 171.BR setstate ()
52f091ee
PH
172T} Thread safety MT-Safe
173.TE
47297adb 174.SH CONFORMING TO
a87a3e17 175POSIX.1-2001, POSIX.1-2008, 4.3BSD.
fea681da 176.SH NOTES
a689fdb2 177The
5b475602 178.BR random ()
a689fdb2
MK
179function should not be used in multithreaded programs
180where reproducible behavior is required.
5b475602
MK
181Use
182.BR random_r (3)
183for that purpose.
184.PP
185Random-number generation is a complex topic.
186.I Numerical Recipes in C: The Art of Scientific Computing
187(William H. Press, Brian P. Flannery, Saul A. Teukolsky, William
188T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.)
189provides an excellent discussion of practical random-number generation
190issues in Chapter 7 (Random Numbers).
191.PP
192For a more theoretical discussion which also covers many practical issues
193in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's
194.IR "The Art of Computer Programming" ,
195volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
196Addison-Wesley Publishing Company, 1981.
e6183193
MK
197.SH BUGS
198According to POSIX,
199.BR initstate ()
200should return NULL on error.
201In the glibc implementation,
202.I errno
203is (as specified) set on error, but the function does not return NULL.
204.\" http://sourceware.org/bugzilla/show_bug.cgi?id=15380
47297adb 205.SH SEE ALSO
d929b801 206.BR getrandom (2),
0125c257 207.BR drand48 (3),
fea681da 208.BR rand (3),
2d64f60e 209.BR random_r (3),
fea681da 210.BR srand (3)