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