summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/random.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/random.3')
-rw-r--r--src/lib/libc/stdlib/random.3190
1 files changed, 190 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/random.3 b/src/lib/libc/stdlib/random.3
new file mode 100644
index 0000000000..3d4545651b
--- /dev/null
+++ b/src/lib/libc/stdlib/random.3
@@ -0,0 +1,190 @@
1.\" Copyright (c) 1983, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\" must display the following acknowledgement:
14.\" This product includes software developed by the University of
15.\" California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: random.3,v 1.14 2001/09/06 15:04:34 mpech Exp $
33.\"
34.Dd April 19, 1991
35.Dt RANDOM 3
36.Os
37.Sh NAME
38.Nm random ,
39.Nm srandom ,
40.Nm srandomdev ,
41.Nm initstate ,
42.Nm setstate
43.Nd better random number generator; routines for changing generators
44.Sh SYNOPSIS
45.Fd #include <stdlib.h>
46.Ft long
47.Fn random void
48.Ft void
49.Fn srandom "unsigned int seed"
50.Ft void
51.Fn srandomdev void
52.Ft char *
53.Fn initstate "unsigned int seed" "char *state" "size_t n"
54.Ft char *
55.Fn setstate "const char *state"
56.Sh DESCRIPTION
57The
58.Fn random
59function uses a non-linear additive feedback random number generator employing
60a default table of size 31 long integers to return successive pseudo-random
61numbers in the range from 0 to (2**31)\-1.
62The period of this random number generator is very large, approximately
6316*((2**31)\-1.
64.Pp
65The
66.Fn random
67and
68.Fn srandom
69functions have (almost) the same calling sequence and initialization
70properties as
71.Xr rand 3 Ns / Xr srand 3 .
72The difference is that
73.Xr rand
74produces a much less random sequence \(em in fact, the low dozen bits
75generated by rand go through a cyclic pattern.
76All the bits generated by
77.Fn random
78are usable.
79For example,
80.Sq Li random()&01
81will produce a random binary
82value.
83.Pp
84Like
85.Xr rand 3 ,
86.Fn random
87will by default produce a sequence of numbers that can be duplicated
88by calling
89.Fn srandom
90with
91.Ql 1
92as the seed.
93.Pp
94The
95.Fn srandomdev
96routine initialize a state array using the
97.Xr arandom 4
98random number device which returns good random numbers,
99suitable for cryptographic use.
100Note that this particular seeding procedure can generate
101states which are impossible to reproduce by calling
102.Fn srandom
103with any value, since the succeeding terms in the
104state buffer are no longer derived from the LC algorithm applied to
105a fixed seed.
106.Pp
107The
108.Fn initstate
109routine allows a state array, passed in as an argument, to be initialized
110for future use.
111The size of the state array (in bytes) is used by
112.Fn initstate
113to decide how sophisticated a random number generator it should use \(em the
114more state, the better the random numbers will be.
115(Current "optimal" values for the amount of state information are
1168, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
117the nearest known amount.
118Using less than 8 bytes will cause an error.)
119The seed for the initialization (which specifies a starting point for
120the random number sequence, and provides for restarting at the same
121point) is also an argument.
122The
123.Fn initstate
124function returns a pointer to the previous state information array.
125.Pp
126Once a state has been initialized, the
127.Fn setstate
128routine provides for rapid switching between states.
129The
130.Fn setstate
131function returns a pointer to the previous state array; its
132argument state array is used for further random number generation
133until the next call to
134.Fn initstate
135or
136.Fn setstate .
137.Pp
138Once a state array has been initialized, it may be restarted at a
139different point either by calling
140.Fn initstate
141(with the desired seed, the state array, and its size) or by calling
142both
143.Fn setstate
144(with the state array) and
145.Fn srandom
146(with the desired seed).
147The advantage of calling both
148.Fn setstate
149and
150.Fn srandom
151is that the size of the state array does not have to be remembered after
152it is initialized.
153.Pp
154With 256 bytes of state information, the period of the random number
155generator is greater than 2**69
156which should be sufficient for most purposes.
157.Sh AUTHORS
158.An Earl T. Cohen
159.Sh DIAGNOSTICS
160If
161.Fn initstate
162is called with less than 8 bytes of state information, or if
163.Fn setstate
164detects that the state information has been garbled, error
165messages are printed on the standard error output.
166.Sh SEE ALSO
167.Xr arc4random 3 ,
168.Xr drand48 3 ,
169.Xr rand 3 ,
170.Xr random 4
171.Sh STANDARDS
172The
173.Fn random ,
174.Fn srandom ,
175.Fn initstate ,
176and
177.Fn setstate
178functions conform to
179.St -xpg4.2 .
180.Pp
181The
182.Fn srandomdev
183function is an extension.
184.Sh HISTORY
185These
186functions appeared in
187.Bx 4.2 .
188.Sh BUGS
189About 2/3 the speed of
190.Xr rand 3 .