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.3105
1 files changed, 64 insertions, 41 deletions
diff --git a/src/lib/libc/stdlib/random.3 b/src/lib/libc/stdlib/random.3
index 38c15a9803..3d4545651b 100644
--- a/src/lib/libc/stdlib/random.3
+++ b/src/lib/libc/stdlib/random.3
@@ -29,97 +29,106 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" from: @(#)random.3 6.5 (Berkeley) 4/19/91 32.\" $OpenBSD: random.3,v 1.14 2001/09/06 15:04:34 mpech Exp $
33.\" $Id: random.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $
34.\" 33.\"
35.Dd April 19, 1991 34.Dd April 19, 1991
36.Dt RANDOM 3 35.Dt RANDOM 3
37.Os BSD 4.2 36.Os
38.Sh NAME 37.Sh NAME
39.Nm random , 38.Nm random ,
40.Nm srandom , 39.Nm srandom ,
40.Nm srandomdev ,
41.Nm initstate , 41.Nm initstate ,
42.Nm setstate 42.Nm setstate
43.Nd better random number generator; routines for changing generators 43.Nd better random number generator; routines for changing generators
44.Sh SYNOPSIS 44.Sh SYNOPSIS
45.Fd #include <stdlib.h> 45.Fd #include <stdlib.h>
46.Ft long 46.Ft long
47.Fn random void 47.Fn random void
48.Ft void 48.Ft void
49.Fn srandom "unsigned seed" 49.Fn srandom "unsigned int seed"
50.Ft void
51.Fn srandomdev void
50.Ft char * 52.Ft char *
51.Fn initstate "unsigned seed" "char *state" "int n" 53.Fn initstate "unsigned int seed" "char *state" "size_t n"
52.Ft char * 54.Ft char *
53.Fn setstate "char *state" 55.Fn setstate "const char *state"
54.Sh DESCRIPTION 56.Sh DESCRIPTION
55The 57The
56.Fn random 58.Fn random
57function 59function uses a non-linear additive feedback random number generator employing
58uses a non-linear additive feedback random number generator employing a 60a default table of size 31 long integers to return successive pseudo-random
59default table of size 31 long integers to return successive pseudo-random 61numbers in the range from 0 to (2**31)\-1.
60numbers in the range from 0 to
61.if t 2\u\s731\s10\d\(mi1.
62.if n (2**31)\(mi1.
63The period of this random number generator is very large, approximately 62The period of this random number generator is very large, approximately
64.if t 16\(mu(2\u\s731\s10\d\(mi1). 6316*((2**31)\-1.
65.if n 16*((2**31)\(mi1).
66.Pp 64.Pp
67The 65The
68.Fn random Ns / Fn srandom 66.Fn random
69have (almost) the same calling sequence and initialization properties as 67and
68.Fn srandom
69functions have (almost) the same calling sequence and initialization
70properties as
70.Xr rand 3 Ns / Xr srand 3 . 71.Xr rand 3 Ns / Xr srand 3 .
71The difference is that 72The difference is that
72.Xr rand 73.Xr rand
73produces a much less random sequence \(em in fact, the low dozen bits 74produces a much less random sequence \(em in fact, the low dozen bits
74generated by rand go through a cyclic pattern. All the bits generated by 75generated by rand go through a cyclic pattern.
76All the bits generated by
75.Fn random 77.Fn random
76are usable. For example, 78are usable.
79For example,
77.Sq Li random()&01 80.Sq Li random()&01
78will produce a random binary 81will produce a random binary
79value. 82value.
80.Pp 83.Pp
81Unlike 84Like
82.Xr srand ,
83.Fn srandom
84does not return the old seed; the reason for this is that the amount of
85state information used is much more than a single word. (Two other
86routines are provided to deal with restarting/changing random
87number generators). Like
88.Xr rand 3 , 85.Xr rand 3 ,
89however,
90.Fn random 86.Fn random
91will by default produce a sequence of numbers that can be duplicated 87will by default produce a sequence of numbers that can be duplicated
92by calling 88by calling
93.Fn srandom 89.Fn srandom
94with 90with
95.Ql 1 91.Ql 1
96as the seed. 92as the seed.
97.Pp 93.Pp
98The 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
99.Fn initstate 108.Fn initstate
100routine allows a state array, passed in as an argument, to be initialized 109routine allows a state array, passed in as an argument, to be initialized
101for future use. The size of the state array (in bytes) is used by 110for future use.
111The size of the state array (in bytes) is used by
102.Fn initstate 112.Fn initstate
103to decide how sophisticated a random number generator it should use \(em the 113to decide how sophisticated a random number generator it should use \(em the
104more state, the better the random numbers will be. 114more state, the better the random numbers will be.
105(Current "optimal" values for the amount of state information are 115(Current "optimal" values for the amount of state information are
1068, 32, 64, 128, and 256 bytes; other amounts will be rounded down to 1168, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
107the nearest known amount. Using less than 8 bytes will cause an error.) 117the nearest known amount.
118Using less than 8 bytes will cause an error.)
108The seed for the initialization (which specifies a starting point for 119The seed for the initialization (which specifies a starting point for
109the random number sequence, and provides for restarting at the same 120the random number sequence, and provides for restarting at the same
110point) is also an argument. 121point) is also an argument.
111The 122The
112.Fn initstate 123.Fn initstate
113function 124function returns a pointer to the previous state information array.
114returns a pointer to the previous state information array.
115.Pp 125.Pp
116Once a state has been initialized, the 126Once a state has been initialized, the
117.Fn setstate 127.Fn setstate
118routine provides for rapid switching between states. 128routine provides for rapid switching between states.
119The 129The
120.Fn setstate 130.Fn setstate
121function 131function returns a pointer to the previous state array; its
122returns a pointer to the previous state array; its
123argument state array is used for further random number generation 132argument state array is used for further random number generation
124until the next call to 133until the next call to
125.Fn initstate 134.Fn initstate
@@ -143,12 +152,10 @@ is that the size of the state array does not have to be remembered after
143it is initialized. 152it is initialized.
144.Pp 153.Pp
145With 256 bytes of state information, the period of the random number 154With 256 bytes of state information, the period of the random number
146generator is greater than 155generator is greater than 2**69
147.if t 2\u\s769\s10\d,
148.if n 2**69
149which should be sufficient for most purposes. 156which should be sufficient for most purposes.
150.Sh AUTHOR 157.Sh AUTHORS
151Earl T. Cohen 158.An Earl T. Cohen
152.Sh DIAGNOSTICS 159.Sh DIAGNOSTICS
153If 160If
154.Fn initstate 161.Fn initstate
@@ -157,10 +164,26 @@ is called with less than 8 bytes of state information, or if
157detects that the state information has been garbled, error 164detects that the state information has been garbled, error
158messages are printed on the standard error output. 165messages are printed on the standard error output.
159.Sh SEE ALSO 166.Sh SEE ALSO
160.Xr rand 3 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.
161.Sh HISTORY 184.Sh HISTORY
162These 185These
163functions appeared in 186functions appeared in
164.Bx 4.2 . 187.Bx 4.2 .
165.Sh BUGS 188.Sh BUGS
166About 2/3 the speed of 189About 2/3 the speed of