diff options
Diffstat (limited to 'src/lib/libc/stdlib/random.3')
-rw-r--r-- | src/lib/libc/stdlib/random.3 | 190 |
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 | ||
57 | The | ||
58 | .Fn random | ||
59 | function uses a non-linear additive feedback random number generator employing | ||
60 | a default table of size 31 long integers to return successive pseudo-random | ||
61 | numbers in the range from 0 to (2**31)\-1. | ||
62 | The period of this random number generator is very large, approximately | ||
63 | 16*((2**31)\-1. | ||
64 | .Pp | ||
65 | The | ||
66 | .Fn random | ||
67 | and | ||
68 | .Fn srandom | ||
69 | functions have (almost) the same calling sequence and initialization | ||
70 | properties as | ||
71 | .Xr rand 3 Ns / Xr srand 3 . | ||
72 | The difference is that | ||
73 | .Xr rand | ||
74 | produces a much less random sequence \(em in fact, the low dozen bits | ||
75 | generated by rand go through a cyclic pattern. | ||
76 | All the bits generated by | ||
77 | .Fn random | ||
78 | are usable. | ||
79 | For example, | ||
80 | .Sq Li random()&01 | ||
81 | will produce a random binary | ||
82 | value. | ||
83 | .Pp | ||
84 | Like | ||
85 | .Xr rand 3 , | ||
86 | .Fn random | ||
87 | will by default produce a sequence of numbers that can be duplicated | ||
88 | by calling | ||
89 | .Fn srandom | ||
90 | with | ||
91 | .Ql 1 | ||
92 | as the seed. | ||
93 | .Pp | ||
94 | The | ||
95 | .Fn srandomdev | ||
96 | routine initialize a state array using the | ||
97 | .Xr arandom 4 | ||
98 | random number device which returns good random numbers, | ||
99 | suitable for cryptographic use. | ||
100 | Note that this particular seeding procedure can generate | ||
101 | states which are impossible to reproduce by calling | ||
102 | .Fn srandom | ||
103 | with any value, since the succeeding terms in the | ||
104 | state buffer are no longer derived from the LC algorithm applied to | ||
105 | a fixed seed. | ||
106 | .Pp | ||
107 | The | ||
108 | .Fn initstate | ||
109 | routine allows a state array, passed in as an argument, to be initialized | ||
110 | for future use. | ||
111 | The size of the state array (in bytes) is used by | ||
112 | .Fn initstate | ||
113 | to decide how sophisticated a random number generator it should use \(em the | ||
114 | more state, the better the random numbers will be. | ||
115 | (Current "optimal" values for the amount of state information are | ||
116 | 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to | ||
117 | the nearest known amount. | ||
118 | Using less than 8 bytes will cause an error.) | ||
119 | The seed for the initialization (which specifies a starting point for | ||
120 | the random number sequence, and provides for restarting at the same | ||
121 | point) is also an argument. | ||
122 | The | ||
123 | .Fn initstate | ||
124 | function returns a pointer to the previous state information array. | ||
125 | .Pp | ||
126 | Once a state has been initialized, the | ||
127 | .Fn setstate | ||
128 | routine provides for rapid switching between states. | ||
129 | The | ||
130 | .Fn setstate | ||
131 | function returns a pointer to the previous state array; its | ||
132 | argument state array is used for further random number generation | ||
133 | until the next call to | ||
134 | .Fn initstate | ||
135 | or | ||
136 | .Fn setstate . | ||
137 | .Pp | ||
138 | Once a state array has been initialized, it may be restarted at a | ||
139 | different point either by calling | ||
140 | .Fn initstate | ||
141 | (with the desired seed, the state array, and its size) or by calling | ||
142 | both | ||
143 | .Fn setstate | ||
144 | (with the state array) and | ||
145 | .Fn srandom | ||
146 | (with the desired seed). | ||
147 | The advantage of calling both | ||
148 | .Fn setstate | ||
149 | and | ||
150 | .Fn srandom | ||
151 | is that the size of the state array does not have to be remembered after | ||
152 | it is initialized. | ||
153 | .Pp | ||
154 | With 256 bytes of state information, the period of the random number | ||
155 | generator is greater than 2**69 | ||
156 | which should be sufficient for most purposes. | ||
157 | .Sh AUTHORS | ||
158 | .An Earl T. Cohen | ||
159 | .Sh DIAGNOSTICS | ||
160 | If | ||
161 | .Fn initstate | ||
162 | is called with less than 8 bytes of state information, or if | ||
163 | .Fn setstate | ||
164 | detects that the state information has been garbled, error | ||
165 | messages 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 | ||
172 | The | ||
173 | .Fn random , | ||
174 | .Fn srandom , | ||
175 | .Fn initstate , | ||
176 | and | ||
177 | .Fn setstate | ||
178 | functions conform to | ||
179 | .St -xpg4.2 . | ||
180 | .Pp | ||
181 | The | ||
182 | .Fn srandomdev | ||
183 | function is an extension. | ||
184 | .Sh HISTORY | ||
185 | These | ||
186 | functions appeared in | ||
187 | .Bx 4.2 . | ||
188 | .Sh BUGS | ||
189 | About 2/3 the speed of | ||
190 | .Xr rand 3 . | ||