diff options
Diffstat (limited to 'src/lib/libc/stdlib/rand48.3')
-rw-r--r-- | src/lib/libc/stdlib/rand48.3 | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/rand48.3 b/src/lib/libc/stdlib/rand48.3 new file mode 100644 index 0000000000..0e8ca5389b --- /dev/null +++ b/src/lib/libc/stdlib/rand48.3 | |||
@@ -0,0 +1,165 @@ | |||
1 | .\" Copyright (c) 1993 Martin Birgmeier | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" You may redistribute unmodified or modified versions of this source | ||
5 | .\" code provided that the above copyright notice and this and the | ||
6 | .\" following conditions are retained. | ||
7 | .\" | ||
8 | .\" This software is provided ``as is'', and comes with no warranties | ||
9 | .\" of any kind. I shall in no event be liable for anything that happens | ||
10 | .\" to anyone/anything when using this software. | ||
11 | .\" | ||
12 | .\" $OpenBSD: rand48.3,v 1.11 2007/05/31 19:19:31 jmc Exp $ | ||
13 | .\" | ||
14 | .Dd $Mdocdate: May 31 2007 $ | ||
15 | .Dt RAND48 3 | ||
16 | .Os | ||
17 | .Sh NAME | ||
18 | .Nm drand48 , | ||
19 | .Nm erand48 , | ||
20 | .Nm lrand48 , | ||
21 | .Nm nrand48 , | ||
22 | .Nm mrand48 , | ||
23 | .Nm jrand48 , | ||
24 | .Nm srand48 , | ||
25 | .Nm seed48 , | ||
26 | .Nm lcong48 | ||
27 | .Nd pseudo-random number generators and initialization routines | ||
28 | .Sh SYNOPSIS | ||
29 | .Fd #include <stdlib.h> | ||
30 | .Ft double | ||
31 | .Fn drand48 void | ||
32 | .Ft double | ||
33 | .Fn erand48 "unsigned short xseed[3]" | ||
34 | .Ft long | ||
35 | .Fn lrand48 void | ||
36 | .Ft long | ||
37 | .Fn nrand48 "unsigned short xseed[3]" | ||
38 | .Ft long | ||
39 | .Fn mrand48 void | ||
40 | .Ft long | ||
41 | .Fn jrand48 "unsigned short xseed[3]" | ||
42 | .Ft void | ||
43 | .Fn srand48 "long seed" | ||
44 | .Ft "unsigned short *" | ||
45 | .Fn seed48 "unsigned short xseed[3]" | ||
46 | .Ft void | ||
47 | .Fn lcong48 "unsigned short p[7]" | ||
48 | .Sh DESCRIPTION | ||
49 | The | ||
50 | .Fn rand48 | ||
51 | family of functions generates pseudo-random numbers using a linear | ||
52 | congruential algorithm working on integers 48 bits in size. | ||
53 | The particular formula employed is | ||
54 | r(n+1) = (a * r(n) + c) mod m | ||
55 | where the default values are | ||
56 | for the multiplicand a = 0xfdeece66d = 25214903917 and | ||
57 | the addend c = 0xb = 11. | ||
58 | The modulus is always fixed at m = 2 ** 48. | ||
59 | r(n) is called the seed of the random number generator. | ||
60 | .Pp | ||
61 | For all the six generator routines described next, the first | ||
62 | computational step is to perform a single iteration of the algorithm. | ||
63 | .Pp | ||
64 | .Fn drand48 | ||
65 | and | ||
66 | .Fn erand48 | ||
67 | return values of type double. | ||
68 | The full 48 bits of r(n+1) are | ||
69 | loaded into the mantissa of the returned value, with the exponent set | ||
70 | such that the values produced lie in the interval [0.0, 1.0]. | ||
71 | .Pp | ||
72 | .Fn lrand48 | ||
73 | and | ||
74 | .Fn nrand48 | ||
75 | return values of type long in the range | ||
76 | [0, 2**31-1]. | ||
77 | The high-order (31) bits of | ||
78 | r(n+1) are loaded into the lower bits of the returned value, with | ||
79 | the topmost (sign) bit set to zero. | ||
80 | .Pp | ||
81 | .Fn mrand48 | ||
82 | and | ||
83 | .Fn jrand48 | ||
84 | return values of type long in the range | ||
85 | [-2**31, 2**31-1]. | ||
86 | The high-order (32) bits of r(n+1) are loaded into the returned value. | ||
87 | .Pp | ||
88 | .Fn drand48 , | ||
89 | .Fn lrand48 , | ||
90 | and | ||
91 | .Fn mrand48 | ||
92 | use an internal buffer to store r(n). | ||
93 | For these functions | ||
94 | the initial value of r(0) = 0x1234abcd330e = 20017429951246. | ||
95 | .Pp | ||
96 | On the other hand, | ||
97 | .Fn erand48 , | ||
98 | .Fn nrand48 , | ||
99 | and | ||
100 | .Fn jrand48 | ||
101 | use a user-supplied buffer to store the seed r(n), | ||
102 | which consists of an array of 3 shorts, where the zeroth member | ||
103 | holds the least significant bits. | ||
104 | .Pp | ||
105 | All functions share the same multiplicand and addend. | ||
106 | .Pp | ||
107 | .Fn srand48 | ||
108 | is used to initialize the internal buffer r(n) of | ||
109 | .Fn drand48 , | ||
110 | .Fn lrand48 , | ||
111 | and | ||
112 | .Fn mrand48 | ||
113 | such that the 32 bits of the seed value are copied into the upper 32 bits | ||
114 | of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e. | ||
115 | Additionally, the constant multiplicand and addend of the algorithm are | ||
116 | reset to the default values given above. | ||
117 | .Pp | ||
118 | .Fn seed48 | ||
119 | also initializes the internal buffer r(n) of | ||
120 | .Fn drand48 , | ||
121 | .Fn lrand48 , | ||
122 | and | ||
123 | .Fn mrand48 , | ||
124 | but here all 48 bits of the seed can be specified in an array of 3 shorts, | ||
125 | where the zeroth member specifies the lowest bits. | ||
126 | Again, the constant multiplicand and addend of the algorithm are | ||
127 | reset to the default values given above. | ||
128 | .Fn seed48 | ||
129 | returns a pointer to an array of 3 shorts which contains the old seed. | ||
130 | This array is statically allocated, so its contents are lost after | ||
131 | each new call to | ||
132 | .Fn seed48 . | ||
133 | .Pp | ||
134 | Finally, | ||
135 | .Fn lcong48 | ||
136 | allows full control over the multiplicand and addend used in | ||
137 | .Fn drand48 , | ||
138 | .Fn erand48 , | ||
139 | .Fn lrand48 , | ||
140 | .Fn nrand48 , | ||
141 | .Fn mrand48 , | ||
142 | and | ||
143 | .Fn jrand48 , | ||
144 | and the seed used in | ||
145 | .Fn drand48 , | ||
146 | .Fn lrand48 , | ||
147 | and | ||
148 | .Fn mrand48 . | ||
149 | An array of 7 shorts is passed as parameter; the first three shorts are | ||
150 | used to initialize the seed; the second three are used to initialize the | ||
151 | multiplicand; and the last short is used to initialize the addend. | ||
152 | It is thus not possible to use values greater than 0xffff as the addend. | ||
153 | .Pp | ||
154 | Note that all three methods of seeding the random number generator | ||
155 | always also set the multiplicand and addend for any of the six | ||
156 | generator calls. | ||
157 | .Pp | ||
158 | For a more powerful random number generator, see | ||
159 | .Xr random 3 . | ||
160 | .Sh SEE ALSO | ||
161 | .Xr arc4random 3 , | ||
162 | .Xr rand 3 , | ||
163 | .Xr random 3 | ||
164 | .Sh AUTHORS | ||
165 | Martin Birgmeier | ||