diff options
Diffstat (limited to 'src/lib/libc/stdlib/random.3')
| -rw-r--r-- | src/lib/libc/stdlib/random.3 | 183 |
1 files changed, 183 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..acfbd8cdeb --- /dev/null +++ b/src/lib/libc/stdlib/random.3 | |||
| @@ -0,0 +1,183 @@ | |||
| 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.6 1998/07/05 19:54:25 millert Exp $ | ||
| 33 | .\" | ||
| 34 | .Dd April 19, 1991 | ||
| 35 | .Dt RANDOM 3 | ||
| 36 | .Os BSD 4.2 | ||
| 37 | .Sh NAME | ||
| 38 | .Nm random , | ||
| 39 | .Nm srandom , | ||
| 40 | .Nm initstate , | ||
| 41 | .Nm setstate | ||
| 42 | .Nd better random number generator; routines for changing generators | ||
| 43 | .Sh SYNOPSIS | ||
| 44 | .Fd #include <stdlib.h> | ||
| 45 | .Ft long | ||
| 46 | .Fn random void | ||
| 47 | .Ft void | ||
| 48 | .Fn srandom "unsigned int seed" | ||
| 49 | .Ft char * | ||
| 50 | .Fn initstate "unsigned int seed" "char *state" "size_t n" | ||
| 51 | .Ft char * | ||
| 52 | .Fn setstate "const char *state" | ||
| 53 | .Sh DESCRIPTION | ||
| 54 | The | ||
| 55 | .Fn random | ||
| 56 | function | ||
| 57 | uses a non-linear additive feedback random number generator employing a | ||
| 58 | default table of size 31 long integers to return successive pseudo-random | ||
| 59 | numbers in the range from 0 to | ||
| 60 | .if t 2\u\s731\s10\d\(mi1. | ||
| 61 | .if n (2**31)\(mi1. | ||
| 62 | The maximum value returned by | ||
| 63 | .Fn random | ||
| 64 | is | ||
| 65 | .Dv LONG_MAX | ||
| 66 | (as defined by the header file | ||
| 67 | .Aq Pa limits.h ) . | ||
| 68 | The period of this random number generator is very large, approximately | ||
| 69 | .if t 16\(mu(2\u\s731\s10\d\(mi1). | ||
| 70 | .if n 16*((2**31)\(mi1). | ||
| 71 | .Pp | ||
| 72 | The | ||
| 73 | .Fn random Ns / Fn srandom | ||
| 74 | have (almost) the same calling sequence and initialization properties as | ||
| 75 | .Xr rand 3 Ns / Xr srand 3 . | ||
| 76 | The difference is that | ||
| 77 | .Xr rand | ||
| 78 | produces a much less random sequence \(em in fact, the low dozen bits | ||
| 79 | generated by rand go through a cyclic pattern. All the bits generated by | ||
| 80 | .Fn random | ||
| 81 | are usable. For example, | ||
| 82 | .Sq Li random()&01 | ||
| 83 | will produce a random binary | ||
| 84 | value. | ||
| 85 | .Pp | ||
| 86 | Unlike | ||
| 87 | .Xr srand , | ||
| 88 | .Fn srandom | ||
| 89 | does not return the old seed; the reason for this is that the amount of | ||
| 90 | state information used is much more than a single word. (Two other | ||
| 91 | routines are provided to deal with restarting/changing random | ||
| 92 | number generators). Like | ||
| 93 | .Xr rand 3 , | ||
| 94 | however, | ||
| 95 | .Fn random | ||
| 96 | will by default produce a sequence of numbers that can be duplicated | ||
| 97 | by calling | ||
| 98 | .Fn srandom | ||
| 99 | with | ||
| 100 | .Ql 1 | ||
| 101 | as the 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. The size of the state array (in bytes) is used by | ||
| 107 | .Fn initstate | ||
| 108 | to decide how sophisticated a random number generator it should use \(em the | ||
| 109 | more state, the better the random numbers will be. | ||
| 110 | (Current "optimal" values for the amount of state information are | ||
| 111 | 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to | ||
| 112 | the nearest known amount. Using less than 8 bytes will cause an error.) | ||
| 113 | The seed for the initialization (which specifies a starting point for | ||
| 114 | the random number sequence, and provides for restarting at the same | ||
| 115 | point) is also an argument. | ||
| 116 | The | ||
| 117 | .Fn initstate | ||
| 118 | function | ||
| 119 | returns a pointer to the previous state information array. | ||
| 120 | .Pp | ||
| 121 | Once a state has been initialized, the | ||
| 122 | .Fn setstate | ||
| 123 | routine provides for rapid switching between states. | ||
| 124 | The | ||
| 125 | .Fn setstate | ||
| 126 | function | ||
| 127 | 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 | ||
| 152 | .if t 2\u\s769\s10\d, | ||
| 153 | .if n 2**69 | ||
| 154 | which should be sufficient for most purposes. | ||
| 155 | .Sh AUTHOR | ||
| 156 | Earl T. Cohen | ||
| 157 | .Sh DIAGNOSTICS | ||
| 158 | If | ||
| 159 | .Fn initstate | ||
| 160 | is called with less than 8 bytes of state information, or if | ||
| 161 | .Fn setstate | ||
| 162 | detects that the state information has been garbled, error | ||
| 163 | messages are printed on the standard error output. | ||
| 164 | .Sh SEE ALSO | ||
| 165 | .Xr arc4random 3 , | ||
| 166 | .Xr drand48 3 , | ||
| 167 | .Xr rand 3 | ||
| 168 | .Sh STANDARDS | ||
| 169 | The | ||
| 170 | .Fn random , | ||
| 171 | .Fn srandom , | ||
| 172 | .Fn initstate , | ||
| 173 | and | ||
| 174 | .Fn setstate | ||
| 175 | functions conform to | ||
| 176 | .St -xpg4.2 . | ||
| 177 | .Sh HISTORY | ||
| 178 | These | ||
| 179 | functions appeared in | ||
| 180 | .Bx 4.2 . | ||
| 181 | .Sh BUGS | ||
| 182 | About 2/3 the speed of | ||
| 183 | .Xr rand 3 . | ||
