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