mount_setattr.2: ffix
[man-pages.git] / man3 / random.3
blobacc154dfe3feeecc39144cc281465bfaa1b5a6cd
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
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.
12 .\"
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.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
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 .\"
33 .TH RANDOM 3  2021-03-22 "GNU" "Linux Programmer's Manual"
34 .SH NAME
35 random, srandom, initstate, setstate \- random number generator
36 .SH SYNOPSIS
37 .nf
38 .B #include <stdlib.h>
39 .PP
40 .B long random(void);
41 .BI "void srandom(unsigned int " seed );
42 .PP
43 .BI "char *initstate(unsigned int " seed ", char *" state ", size_t " n );
44 .BI "char *setstate(char *" state );
45 .fi
46 .PP
47 .RS -4
48 Feature Test Macro Requirements for glibc (see
49 .BR feature_test_macros (7)):
50 .RE
51 .PP
52 .BR random (),
53 .BR srandom (),
54 .BR initstate (),
55 .BR setstate ():
56 .nf
57     _XOPEN_SOURCE >= 500
58 .\"    || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
59         || /* Glibc since 2.19: */ _DEFAULT_SOURCE
60         || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
61 .fi
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 2^31\ \-\ 1.
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\(emthe larger the state array,
96 the better the random numbers will be.
97 Current "optimal" values for the size of the state array \fIn\fP are
98 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
99 the nearest known amount.
100 Using less than 8 bytes results in an error.
101 \fIseed\fP is the seed for the
102 initialization, which specifies a starting point for the random number
103 sequence, and provides for restarting at the same point.
106 .BR setstate ()
107 function changes the state array used by the
108 .BR random ()
109 function.
110 The state array \fIstate\fP is used for
111 random number generation until the next call to
112 .BR initstate ()
114 .BR setstate ().
115 \fIstate\fP must first have been initialized
116 using
117 .BR initstate ()
118 or be the result of a previous call of
119 .BR setstate ().
120 .SH RETURN VALUE
122 .BR random ()
123 function returns a value between 0 and
124 .IR "(2^31)\ \-\ 1" .
126 .BR srandom ()
127 function returns no value.
130 .BR initstate ()
131 function returns a pointer to the previous state array.
132 On failure, it returns NULL, and
133 .I errno
134 is set to indicate the error.
136 On success,
137 .BR setstate ()
138 returns a pointer to the previous state array.
139 On failure, it returns NULL, and
140 .I errno
141 is set to indicate the error.
142 .SH ERRORS
144 .B EINVAL
146 .I state
147 argument given to
148 .BR setstate ()
149 was NULL.
151 .B EINVAL
152 A state array of less than 8 bytes was specified to
153 .BR initstate ().
154 .SH ATTRIBUTES
155 For an explanation of the terms used in this section, see
156 .BR attributes (7).
157 .ad l
160 allbox;
161 lbx lb lb
162 l l l.
163 Interface       Attribute       Value
165 .BR random (),
166 .BR srandom (),
167 .BR initstate (),
168 .BR setstate ()
169 T}      Thread safety   MT-Safe
173 .sp 1
174 .SH CONFORMING TO
175 POSIX.1-2001, POSIX.1-2008, 4.3BSD.
176 .SH NOTES
178 .BR random ()
179 function should not be used in multithreaded programs
180 where reproducible behavior is required.
182 .BR random_r (3)
183 for that purpose.
185 Random-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,
188 William T.\& Vetterling; New York: Cambridge University Press, 2007, 3rd ed.)
189 provides an excellent discussion of practical random-number generation
190 issues in Chapter 7 (Random Numbers).
192 For a more theoretical discussion which also covers many practical issues
193 in depth, see Chapter 3 (Random Numbers) in Donald E.\& Knuth's
194 .IR "The Art of Computer Programming" ,
195 volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
196 Addison-Wesley Publishing Company, 1981.
197 .SH BUGS
198 According to POSIX,
199 .BR initstate ()
200 should return NULL on error.
201 In the glibc implementation,
202 .I errno
203 is (as specified) set on error, but the function does not return NULL.
204 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=15380
205 .SH SEE ALSO
206 .BR getrandom (2),
207 .BR drand48 (3),
208 .BR rand (3),
209 .BR random_r (3),
210 .BR srand (3)