diff options
Diffstat (limited to 'src/lib/libc/stdlib/random.3')
| -rw-r--r-- | src/lib/libc/stdlib/random.3 | 114 |
1 files changed, 66 insertions, 48 deletions
diff --git a/src/lib/libc/stdlib/random.3 b/src/lib/libc/stdlib/random.3 index 38c15a9803..ed05df162b 100644 --- a/src/lib/libc/stdlib/random.3 +++ b/src/lib/libc/stdlib/random.3 | |||
| @@ -9,11 +9,7 @@ | |||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | 9 | .\" 2. Redistributions in binary form must reproduce the above copyright |
| 10 | .\" notice, this list of conditions and the following disclaimer in the | 10 | .\" notice, this list of conditions and the following disclaimer in the |
| 11 | .\" documentation and/or other materials provided with the distribution. | 11 | .\" documentation and/or other materials provided with the distribution. |
| 12 | .\" 3. All advertising materials mentioning features or use of this software | 12 | .\" 3. Neither the name of the University nor the names of its contributors |
| 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 | 13 | .\" may be used to endorse or promote products derived from this software |
| 18 | .\" without specific prior written permission. | 14 | .\" without specific prior written permission. |
| 19 | .\" | 15 | .\" |
| @@ -29,97 +25,105 @@ | |||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | .\" SUCH DAMAGE. | 26 | .\" SUCH DAMAGE. |
| 31 | .\" | 27 | .\" |
| 32 | .\" from: @(#)random.3 6.5 (Berkeley) 4/19/91 | 28 | .\" $OpenBSD: random.3,v 1.19 2007/05/31 19:19:31 jmc Exp $ |
| 33 | .\" $Id: random.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 34 | .\" | 29 | .\" |
| 35 | .Dd April 19, 1991 | 30 | .Dd $Mdocdate: May 31 2007 $ |
| 36 | .Dt RANDOM 3 | 31 | .Dt RANDOM 3 |
| 37 | .Os BSD 4.2 | 32 | .Os |
| 38 | .Sh NAME | 33 | .Sh NAME |
| 39 | .Nm random , | 34 | .Nm random , |
| 40 | .Nm srandom , | 35 | .Nm srandom , |
| 36 | .Nm srandomdev , | ||
| 41 | .Nm initstate , | 37 | .Nm initstate , |
| 42 | .Nm setstate | 38 | .Nm setstate |
| 43 | .Nd better random number generator; routines for changing generators | 39 | .Nd better random number generator; routines for changing generators |
| 44 | .Sh SYNOPSIS | 40 | .Sh SYNOPSIS |
| 45 | .Fd #include <stdlib.h> | 41 | .Fd #include <stdlib.h> |
| 46 | .Ft long | 42 | .Ft long |
| 47 | .Fn random void | 43 | .Fn random void |
| 48 | .Ft void | 44 | .Ft void |
| 49 | .Fn srandom "unsigned seed" | 45 | .Fn srandom "unsigned int seed" |
| 46 | .Ft void | ||
| 47 | .Fn srandomdev void | ||
| 50 | .Ft char * | 48 | .Ft char * |
| 51 | .Fn initstate "unsigned seed" "char *state" "int n" | 49 | .Fn initstate "unsigned int seed" "char *state" "size_t n" |
| 52 | .Ft char * | 50 | .Ft char * |
| 53 | .Fn setstate "char *state" | 51 | .Fn setstate "const char *state" |
| 54 | .Sh DESCRIPTION | 52 | .Sh DESCRIPTION |
| 55 | The | 53 | The |
| 56 | .Fn random | 54 | .Fn random |
| 57 | function | 55 | function uses a non-linear additive feedback random number generator employing |
| 58 | uses a non-linear additive feedback random number generator employing a | 56 | a default table of size 31 long integers to return successive pseudo-random |
| 59 | default table of size 31 long integers to return successive pseudo-random | 57 | numbers in the range from 0 to (2**31)\-1. |
| 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 | 58 | The period of this random number generator is very large, approximately |
| 64 | .if t 16\(mu(2\u\s731\s10\d\(mi1). | 59 | 16*((2**31)\-1). |
| 65 | .if n 16*((2**31)\(mi1). | ||
| 66 | .Pp | 60 | .Pp |
| 67 | The | 61 | The |
| 68 | .Fn random Ns / Fn srandom | 62 | .Fn random |
| 69 | have (almost) the same calling sequence and initialization properties as | 63 | and |
| 70 | .Xr rand 3 Ns / Xr srand 3 . | 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 . | ||
| 71 | The difference is that | 68 | The difference is that |
| 72 | .Xr rand | 69 | .Xr rand |
| 73 | produces a much less random sequence \(em in fact, the low dozen bits | 70 | 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 | 71 | generated by rand go through a cyclic pattern. |
| 72 | All the bits generated by | ||
| 75 | .Fn random | 73 | .Fn random |
| 76 | are usable. For example, | 74 | are usable. |
| 75 | For example, | ||
| 77 | .Sq Li random()&01 | 76 | .Sq Li random()&01 |
| 78 | will produce a random binary | 77 | will produce a random binary |
| 79 | value. | 78 | value. |
| 80 | .Pp | 79 | .Pp |
| 81 | Unlike | 80 | Like |
| 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 , | 81 | .Xr rand 3 , |
| 89 | however, | ||
| 90 | .Fn random | 82 | .Fn random |
| 91 | will by default produce a sequence of numbers that can be duplicated | 83 | will by default produce a sequence of numbers that can be duplicated |
| 92 | by calling | 84 | by calling |
| 93 | .Fn srandom | 85 | .Fn srandom |
| 94 | with | 86 | with |
| 95 | .Ql 1 | 87 | .Ql 1 |
| 96 | as the seed. | 88 | as the seed. |
| 97 | .Pp | 89 | .Pp |
| 98 | The | 90 | The |
| 91 | .Fn srandomdev | ||
| 92 | routine initializes a state array using | ||
| 93 | random numbers obtained from the kernel, | ||
| 94 | suitable for cryptographic use. | ||
| 95 | Note that this particular seeding procedure can generate | ||
| 96 | states which are impossible to reproduce by calling | ||
| 97 | .Fn srandom | ||
| 98 | with any value, since the succeeding terms in the | ||
| 99 | state buffer are no longer derived from the LC algorithm applied to | ||
| 100 | a fixed seed. | ||
| 101 | .Pp | ||
| 102 | The | ||
| 99 | .Fn initstate | 103 | .Fn initstate |
| 100 | routine allows a state array, passed in as an argument, to be initialized | 104 | 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 | 105 | for future use. |
| 106 | The size of the state array (in bytes) is used by | ||
| 102 | .Fn initstate | 107 | .Fn initstate |
| 103 | to decide how sophisticated a random number generator it should use \(em the | 108 | to decide how sophisticated a random number generator it should use \(em the |
| 104 | more state, the better the random numbers will be. | 109 | more state, the better the random numbers will be. |
| 105 | (Current "optimal" values for the amount of state information are | 110 | (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 | 111 | 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.) | 112 | the nearest known amount. |
| 113 | Using less than 8 bytes will cause an error.) | ||
| 108 | The seed for the initialization (which specifies a starting point for | 114 | The seed for the initialization (which specifies a starting point for |
| 109 | the random number sequence, and provides for restarting at the same | 115 | the random number sequence, and provides for restarting at the same |
| 110 | point) is also an argument. | 116 | point) is also an argument. |
| 111 | The | 117 | The |
| 112 | .Fn initstate | 118 | .Fn initstate |
| 113 | function | 119 | function returns a pointer to the previous state information array. |
| 114 | returns a pointer to the previous state information array. | ||
| 115 | .Pp | 120 | .Pp |
| 116 | Once a state has been initialized, the | 121 | Once a state has been initialized, the |
| 117 | .Fn setstate | 122 | .Fn setstate |
| 118 | routine provides for rapid switching between states. | 123 | routine provides for rapid switching between states. |
| 119 | The | 124 | The |
| 120 | .Fn setstate | 125 | .Fn setstate |
| 121 | function | 126 | function returns a pointer to the previous state array; its |
| 122 | returns a pointer to the previous state array; its | ||
| 123 | argument state array is used for further random number generation | 127 | argument state array is used for further random number generation |
| 124 | until the next call to | 128 | until the next call to |
| 125 | .Fn initstate | 129 | .Fn initstate |
| @@ -143,12 +147,8 @@ is that the size of the state array does not have to be remembered after | |||
| 143 | it is initialized. | 147 | it is initialized. |
| 144 | .Pp | 148 | .Pp |
| 145 | With 256 bytes of state information, the period of the random number | 149 | With 256 bytes of state information, the period of the random number |
| 146 | generator is greater than | 150 | generator is greater than 2**69 |
| 147 | .if t 2\u\s769\s10\d, | ||
| 148 | .if n 2**69 | ||
| 149 | which should be sufficient for most purposes. | 151 | which should be sufficient for most purposes. |
| 150 | .Sh AUTHOR | ||
| 151 | Earl T. Cohen | ||
| 152 | .Sh DIAGNOSTICS | 152 | .Sh DIAGNOSTICS |
| 153 | If | 153 | If |
| 154 | .Fn initstate | 154 | .Fn initstate |
| @@ -157,11 +157,29 @@ is called with less than 8 bytes of state information, or if | |||
| 157 | detects that the state information has been garbled, error | 157 | detects that the state information has been garbled, error |
| 158 | messages are printed on the standard error output. | 158 | messages are printed on the standard error output. |
| 159 | .Sh SEE ALSO | 159 | .Sh SEE ALSO |
| 160 | .Xr rand 3 | 160 | .Xr arc4random 3 , |
| 161 | .Xr drand48 3 , | ||
| 162 | .Xr rand 3 , | ||
| 163 | .Xr random 4 | ||
| 164 | .Sh STANDARDS | ||
| 165 | The | ||
| 166 | .Fn random , | ||
| 167 | .Fn srandom , | ||
| 168 | .Fn initstate , | ||
| 169 | and | ||
| 170 | .Fn setstate | ||
| 171 | functions conform to | ||
| 172 | .St -xpg4.2 . | ||
| 173 | .Pp | ||
| 174 | The | ||
| 175 | .Fn srandomdev | ||
| 176 | function is an extension. | ||
| 161 | .Sh HISTORY | 177 | .Sh HISTORY |
| 162 | These | 178 | These |
| 163 | functions appeared in | 179 | functions appeared in |
| 164 | .Bx 4.2 . | 180 | .Bx 4.2 . |
| 181 | .Sh AUTHORS | ||
| 182 | .An Earl T. Cohen | ||
| 165 | .Sh BUGS | 183 | .Sh BUGS |
| 166 | About 2/3 the speed of | 184 | About 2/3 the speed of |
| 167 | .Xr rand 3 . | 185 | .Xr rand 3 . |
