1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
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
33 .TH RANDOM 3 2010-09-20 "GNU" "Linux Programmer's Manual"
35 random, srandom, initstate, setstate \- random number generator
38 .B #include <stdlib.h>
40 .B long int random(void);
42 .BI "void srandom(unsigned int " seed );
44 .BI "char *initstate(unsigned int " seed ", char *" state ", size_t " n );
46 .BI "char *setstate(char *" state );
50 Feature Test Macro Requirements for glibc (see
51 .BR feature_test_macros (7)):
60 _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
61 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
67 function uses a nonlinear additive feedback random
68 number generator employing a default table of size 31 long integers to
69 return successive pseudo-random numbers in
70 the range from 0 to \fBRAND_MAX\fR.
71 The period of this random number generator is very large, approximately
72 .IR "16\ *\ ((2^31)\ \-\ 1)" .
76 function sets its argument as the seed for a new
77 sequence of pseudo-random integers to be returned by
79 These sequences are repeatable by calling
83 If no seed value is provided, the
86 is automatically seeded with a value of 1.
90 function allows a state array \fIstate\fP to
91 be initialized for use by
93 The size of the state array
96 to decide how sophisticated a
97 random number generator it should use\(emthe larger the state array,
98 the better the random numbers will be.
99 \fIseed\fP is the seed for the
100 initialization, which specifies a starting point for the random number
101 sequence, and provides for restarting at the same point.
105 function changes the state array used by the
108 The state array \fIstate\fP is used for
109 random number generation until the next call to
113 \fIstate\fP must first have been initialized
116 or be the result of a previous call of
121 function returns a value between 0 and
125 function returns no value.
128 function returns a pointer to the previous state array.
131 function returns a pointer to the previous state array, or NULL on error.
135 A state array of less than 8 bytes was specified to
138 4.3BSD, POSIX.1-2001.
140 Current "optimal" values for the size of the state array \fIn\fP are
141 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
142 the nearest known amount.
143 Using less than 8 bytes will cause an
146 This function should not be used in cases where multiple threads use
148 and the behavior should be reproducible.
153 Random-number generation is a complex topic.
154 .I Numerical Recipes in C: The Art of Scientific Computing
155 (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William
156 T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.)
157 provides an excellent discussion of practical random-number generation
158 issues in Chapter 7 (Random Numbers).
160 For a more theoretical discussion which also covers many practical issues
161 in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's
162 .IR "The Art of Computer Programming" ,
163 volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
164 Addison-Wesley Publishing Company, 1981.