]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/random.3
getauxval.3: wfix
[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.\"
460495ca 33.TH RANDOM 3 2015-08-08 "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>
39.sp
40.B long int random(void);
5895e7eb 41
fea681da 42.BI "void srandom(unsigned int " seed );
5895e7eb 43
fea681da 44.BI "char *initstate(unsigned int " seed ", char *" state ", size_t " n );
defcceb3 45.br
fea681da
MK
46.BI "char *setstate(char *" state );
47.fi
cc4615cc
MK
48.sp
49.in -4n
50Feature Test Macro Requirements for glibc (see
51.BR feature_test_macros (7)):
52.in
53.sp
54.ad l
55.BR random (),
56.BR srandom (),
57.BR initstate (),
58.BR setstate ():
bb1029be
MK
59.RS 4
60_SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
61_XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
62.RE
63.ad
fea681da 64.SH DESCRIPTION
60a90ecd
MK
65The
66.BR random ()
fb0e9c48 67function uses a nonlinear additive feedback random
fea681da 68number generator employing a default table of size 31 long integers to
c13182ef
MK
69return successive pseudo-random numbers in
70the range from 0 to \fBRAND_MAX\fR.
fea681da 71The period of this random number generator is very large, approximately
b983ad10 72.IR "16\ *\ ((2^31)\ \-\ 1)" .
fea681da 73.PP
60a90ecd
MK
74The
75.BR srandom ()
76function sets its argument as the seed for a new
77sequence of pseudo-random integers to be returned by
78.BR random ().
79These sequences are repeatable by calling
80.BR srandom ()
81with the same
c13182ef 82seed value.
60a90ecd
MK
83If no seed value is provided, the
84.BR random ()
85function
fea681da
MK
86is automatically seeded with a value of 1.
87.PP
60a90ecd
MK
88The
89.BR initstate ()
90function allows a state array \fIstate\fP to
91be initialized for use by
92.BR random ().
c13182ef 93The size of the state array
60a90ecd
MK
94\fIn\fP is used by
95.BR initstate ()
96to decide how sophisticated a
5503c85e 97random number generator it should use\(emthe larger the state array,
be7fff26
MK
98the better the random numbers will be.
99\fIseed\fP is the seed for the
fea681da
MK
100initialization, which specifies a starting point for the random number
101sequence, and provides for restarting at the same point.
102.PP
60a90ecd
MK
103The
104.BR setstate ()
105function changes the state array used by the
106.BR random ()
107function.
c13182ef 108The state array \fIstate\fP is used for
60a90ecd
MK
109random number generation until the next call to
110.BR initstate ()
111or
112.BR setstate ().
113\fIstate\fP must first have been initialized
114using
115.BR initstate ()
116or be the result of a previous call of
117.BR setstate ().
47297adb 118.SH RETURN VALUE
60a90ecd
MK
119The
120.BR random ()
2f0af33b
MK
121function returns a value between 0 and
122.BR RAND_MAX .
60a90ecd
MK
123The
124.BR srandom ()
125function returns no value.
6d1a3ba6 126
60a90ecd
MK
127The
128.BR initstate ()
247cb8f5 129function returns a pointer to the previous state array.
6d1a3ba6
MK
130On error,
131.I errno
132is set to indicate the cause.
133
134On success,
60a90ecd 135.BR setstate ()
6d1a3ba6
MK
136returns a pointer to the previous state array.
137On error, it returns NULL, with
138.I errno
139set to indicate the cause of the error.
fea681da
MK
140.SH ERRORS
141.TP
142.B EINVAL
d2fdbe9c
MK
143The
144.I state
145argument given to
146.BR setstate ()
147was NULL.
148.TP
149.B EINVAL
60a90ecd
MK
150A state array of less than 8 bytes was specified to
151.BR initstate ().
a04d1466 152.SH ATTRIBUTES
52f091ee
PH
153For an explanation of the terms used in this section, see
154.BR attributes (7).
155.TS
156allbox;
157lbw23 lb lb
158l l l.
159Interface Attribute Value
160T{
a04d1466
PH
161.BR random (),
162.BR srandom (),
52f091ee 163.br
a04d1466 164.BR initstate (),
a04d1466 165.BR setstate ()
52f091ee
PH
166T} Thread safety MT-Safe
167.TE
47297adb 168.SH CONFORMING TO
a87a3e17 169POSIX.1-2001, POSIX.1-2008, 4.3BSD.
fea681da
MK
170.SH NOTES
171Current "optimal" values for the size of the state array \fIn\fP are
1728, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
c13182ef
MK
173the nearest known amount.
174Using less than 8 bytes will cause an
fea681da 175error.
5b475602
MK
176.PP
177This function should not be used in cases where multiple threads use
178.BR random ()
179and the behavior should be reproducible.
180Use
181.BR random_r (3)
182for that purpose.
183.PP
184Random-number generation is a complex topic.
185.I Numerical Recipes in C: The Art of Scientific Computing
186(William H. Press, Brian P. Flannery, Saul A. Teukolsky, William
187T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.)
188provides an excellent discussion of practical random-number generation
189issues in Chapter 7 (Random Numbers).
190.PP
191For a more theoretical discussion which also covers many practical issues
192in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's
193.IR "The Art of Computer Programming" ,
194volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
195Addison-Wesley Publishing Company, 1981.
e6183193
MK
196.SH BUGS
197According to POSIX,
198.BR initstate ()
199should return NULL on error.
200In the glibc implementation,
201.I errno
202is (as specified) set on error, but the function does not return NULL.
203.\" http://sourceware.org/bugzilla/show_bug.cgi?id=15380
47297adb 204.SH SEE ALSO
5b475602 205.BR drand48 (3),
d929b801 206.BR getrandom (2),
fea681da 207.BR rand (3),
2d64f60e 208.BR random_r (3),
fea681da 209.BR srand (3)