]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/random.3
ffix
[thirdparty/man-pages.git] / man3 / random.3
CommitLineData
fea681da
MK
1.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2.\"
3.\" Permission is granted to make and distribute verbatim copies of this
4.\" manual provided the copyright notice and this permission notice are
5.\" preserved on all copies.
6.\"
7.\" Permission is granted to copy and distribute modified versions of this
8.\" manual under the conditions for verbatim copying, provided that the
9.\" entire resulting derived work is distributed under the terms of a
10.\" permission notice identical to this one.
c13182ef 11.\"
fea681da
MK
12.\" Since the Linux kernel and libraries are constantly changing, this
13.\" manual page may be incorrect or out-of-date. The author(s) assume no
14.\" responsibility for errors or omissions, or for damages resulting from
15.\" the use of the information contained herein. The author(s) may not
16.\" have taken the same level of care in the production of this manual,
17.\" which is licensed free of charge, as they might when working
18.\" professionally.
c13182ef 19.\"
fea681da
MK
20.\" Formatted or processed versions of this manual, if unaccompanied by
21.\" the source, must acknowledge the copyright and authors of this work.
22.\"
23.\" References consulted:
24.\" Linux libc source code
25.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26.\" 386BSD man pages
27.\" Modified Sun Mar 28 00:25:51 1993, David Metcalfe
28.\" Modified Sat Jul 24 18:13:39 1993 by Rik Faith (faith@cs.unc.edu)
29.\" Modified Sun Aug 20 21:47:07 2000, aeb
30.\"
31.TH RANDOM 3 2000-08-20 "GNU" "Linux Programmer's Manual"
32.SH NAME
33random, srandom, initstate, setstate \- random number generator
34.SH SYNOPSIS
35.nf
36.B #include <stdlib.h>
37.sp
38.B long int random(void);
defcceb3 39.br
fea681da 40.BI "void srandom(unsigned int " seed );
defcceb3 41.br
fea681da 42.BI "char *initstate(unsigned int " seed ", char *" state ", size_t " n );
defcceb3 43.br
fea681da
MK
44.BI "char *setstate(char *" state );
45.fi
46.SH DESCRIPTION
60a90ecd
MK
47The
48.BR random ()
49function uses a non-linear additive feedback random
fea681da 50number generator employing a default table of size 31 long integers to
c13182ef
MK
51return successive pseudo-random numbers in
52the range from 0 to \fBRAND_MAX\fR.
fea681da
MK
53The period of this random number generator is very large, approximately
5416*((2**31)\-1).
55.PP
60a90ecd
MK
56The
57.BR srandom ()
58function sets its argument as the seed for a new
59sequence of pseudo-random integers to be returned by
60.BR random ().
61These sequences are repeatable by calling
62.BR srandom ()
63with the same
c13182ef 64seed value.
60a90ecd
MK
65If no seed value is provided, the
66.BR random ()
67function
fea681da
MK
68is automatically seeded with a value of 1.
69.PP
60a90ecd
MK
70The
71.BR initstate ()
72function allows a state array \fIstate\fP to
73be initialized for use by
74.BR random ().
c13182ef 75The size of the state array
60a90ecd
MK
76\fIn\fP is used by
77.BR initstate ()
78to decide how sophisticated a
fea681da
MK
79random number generator it should use \(em the larger the state array,
80the better the random numbers will be. \fIseed\fP is the seed for the
81initialization, which specifies a starting point for the random number
82sequence, and provides for restarting at the same point.
83.PP
60a90ecd
MK
84The
85.BR setstate ()
86function changes the state array used by the
87.BR random ()
88function.
c13182ef 89The state array \fIstate\fP is used for
60a90ecd
MK
90random number generation until the next call to
91.BR initstate ()
92or
93.BR setstate ().
94\fIstate\fP must first have been initialized
95using
96.BR initstate ()
97or be the result of a previous call of
98.BR setstate ().
fea681da 99.SH "RETURN VALUE"
60a90ecd
MK
100The
101.BR random ()
102function returns a value between 0 and RAND_MAX.
103The
104.BR srandom ()
105function returns no value.
106The
107.BR initstate ()
108and
109.BR setstate ()
110functions return a pointer to the previous state
fea681da
MK
111array, or NULL on error.
112.SH ERRORS
113.TP
114.B EINVAL
60a90ecd
MK
115A state array of less than 8 bytes was specified to
116.BR initstate ().
fea681da
MK
117.SH NOTES
118Current "optimal" values for the size of the state array \fIn\fP are
1198, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
c13182ef
MK
120the nearest known amount.
121Using less than 8 bytes will cause an
fea681da
MK
122error.
123.SH "CONFORMING TO"
68e1685c 1244.3BSD, POSIX.1-2001.
fea681da
MK
125.SH "SEE ALSO"
126.BR rand (3),
127.BR srand (3)