]>
Commit | Line | Data |
---|---|---|
5b2abdfb A |
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 | .\" @(#)rand48.3 V1.0 MB 8 Oct 1993 | |
1f2f436a | 13 | .\" $FreeBSD: src/lib/libc/gen/rand48.3,v 1.17 2005/01/20 09:17:02 ru Exp $ |
5b2abdfb A |
14 | .\" |
15 | .Dd October 8, 1993 | |
16 | .Dt RAND48 3 | |
17 | .Os | |
18 | .Sh NAME | |
19 | .Nm drand48 , | |
20 | .Nm erand48 , | |
ad3c9f2a A |
21 | .Nm jrand48 , |
22 | .Nm lcong48 , | |
5b2abdfb | 23 | .Nm lrand48 , |
5b2abdfb | 24 | .Nm mrand48 , |
ad3c9f2a | 25 | .Nm nrand48 , |
5b2abdfb | 26 | .Nm seed48 , |
ad3c9f2a | 27 | .Nm srand48 |
5b2abdfb A |
28 | .Nd pseudo random number generators and initialization routines |
29 | .Sh LIBRARY | |
30 | .Lb libc | |
31 | .Sh SYNOPSIS | |
32 | .In stdlib.h | |
33 | .Ft double | |
ad3c9f2a A |
34 | .Fo drand48 |
35 | .Fa void | |
36 | .Fc | |
5b2abdfb | 37 | .Ft double |
ad3c9f2a A |
38 | .Fo erand48 |
39 | .Fa "unsigned short xsubi[3]" | |
40 | .Fc | |
5b2abdfb | 41 | .Ft long |
ad3c9f2a A |
42 | .Fo jrand48 |
43 | .Fa "unsigned short xsubi[3]" | |
44 | .Fc | |
45 | .Ft void | |
46 | .Fo lcong48 | |
47 | .Fa "unsigned short param[7]" | |
48 | .Fc | |
5b2abdfb | 49 | .Ft long |
ad3c9f2a A |
50 | .Fo lrand48 |
51 | .Fa void | |
52 | .Fc | |
5b2abdfb | 53 | .Ft long |
ad3c9f2a A |
54 | .Fo mrand48 |
55 | .Fa void | |
56 | .Fc | |
5b2abdfb | 57 | .Ft long |
ad3c9f2a A |
58 | .Fo nrand48 |
59 | .Fa "unsigned short xsubi[3]" | |
60 | .Fc | |
5b2abdfb | 61 | .Ft "unsigned short *" |
ad3c9f2a A |
62 | .Fo seed48 |
63 | .Fa "unsigned short seed16v[3]" | |
64 | .Fc | |
5b2abdfb | 65 | .Ft void |
ad3c9f2a A |
66 | .Fo srand48 |
67 | .Fa "long seedval" | |
68 | .Fc | |
5b2abdfb A |
69 | .Sh DESCRIPTION |
70 | The | |
71 | .Fn rand48 | |
ad3c9f2a | 72 | family of functions generates pseudo-random numbers, using a linear |
5b2abdfb A |
73 | congruential algorithm working on integers 48 bits in size. |
74 | The | |
75 | particular formula employed is | |
ad3c9f2a A |
76 | r(n+1) = (a * r(n) + c) mod m. |
77 | The default value for the multiplicand `a' is 0x5deece66d (25214903917). | |
78 | The default value for the the addend `c' is 0xb (11). | |
5b2abdfb A |
79 | The modulo is always fixed at m = 2 ** 48. |
80 | r(n) is called the seed of the random number generator. | |
81 | .Pp | |
ad3c9f2a | 82 | For the six generator routines described next, the first |
5b2abdfb A |
83 | computational step is to perform a single iteration of the algorithm. |
84 | .Pp | |
9385eb3d | 85 | The |
5b2abdfb A |
86 | .Fn drand48 |
87 | and | |
88 | .Fn erand48 | |
9385eb3d | 89 | functions |
5b2abdfb A |
90 | return values of type double. |
91 | The full 48 bits of r(n+1) are | |
92 | loaded into the mantissa of the returned value, with the exponent set | |
93 | such that the values produced lie in the interval [0.0, 1.0). | |
94 | .Pp | |
9385eb3d | 95 | The |
5b2abdfb A |
96 | .Fn lrand48 |
97 | and | |
98 | .Fn nrand48 | |
9385eb3d | 99 | functions |
5b2abdfb | 100 | return values of type long in the range |
3d9156a7 A |
101 | [0, 2**31-1]. |
102 | The high-order (31) bits of | |
5b2abdfb A |
103 | r(n+1) are loaded into the lower bits of the returned value, with |
104 | the topmost (sign) bit set to zero. | |
105 | .Pp | |
9385eb3d | 106 | The |
5b2abdfb A |
107 | .Fn mrand48 |
108 | and | |
109 | .Fn jrand48 | |
9385eb3d | 110 | functions |
5b2abdfb | 111 | return values of type long in the range |
3d9156a7 A |
112 | [-2**31, 2**31-1]. |
113 | The high-order (32) bits of | |
5b2abdfb A |
114 | r(n+1) are loaded into the returned value. |
115 | .Pp | |
9385eb3d | 116 | The |
5b2abdfb A |
117 | .Fn drand48 , |
118 | .Fn lrand48 , | |
119 | and | |
120 | .Fn mrand48 | |
9385eb3d | 121 | functions |
3d9156a7 A |
122 | use an internal buffer to store r(n). |
123 | For these functions | |
5b2abdfb A |
124 | the initial value of r(0) = 0x1234abcd330e = 20017429951246. |
125 | .Pp | |
126 | On the other hand, | |
127 | .Fn erand48 , | |
128 | .Fn nrand48 , | |
129 | and | |
130 | .Fn jrand48 | |
131 | use a user-supplied buffer to store the seed r(n), | |
132 | which consists of an array of 3 shorts, where the zeroth member | |
133 | holds the least significant bits. | |
134 | .Pp | |
135 | All functions share the same multiplicand and addend. | |
136 | .Pp | |
9385eb3d | 137 | The |
5b2abdfb | 138 | .Fn srand48 |
9385eb3d | 139 | function |
5b2abdfb A |
140 | is used to initialize the internal buffer r(n) of |
141 | .Fn drand48 , | |
142 | .Fn lrand48 , | |
143 | and | |
ad3c9f2a | 144 | .Fn mrand48 , |
5b2abdfb A |
145 | such that the 32 bits of the seed value are copied into the upper 32 bits |
146 | of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e. | |
147 | Additionally, the constant multiplicand and addend of the algorithm are | |
148 | reset to the default values given above. | |
149 | .Pp | |
9385eb3d | 150 | The |
5b2abdfb | 151 | .Fn seed48 |
9385eb3d | 152 | function |
5b2abdfb A |
153 | also initializes the internal buffer r(n) of |
154 | .Fn drand48 , | |
155 | .Fn lrand48 , | |
156 | and | |
157 | .Fn mrand48 , | |
158 | but here all 48 bits of the seed can be specified in an array of 3 shorts, | |
159 | where the zeroth member specifies the lowest bits. | |
160 | Again, | |
161 | the constant multiplicand and addend of the algorithm are | |
162 | reset to the default values given above. | |
9385eb3d | 163 | The |
5b2abdfb | 164 | .Fn seed48 |
9385eb3d | 165 | function |
5b2abdfb | 166 | returns a pointer to an array of 3 shorts which contains the old seed. |
ad3c9f2a | 167 | This array is statically allocated; thus, its contents are lost after |
5b2abdfb A |
168 | each new call to |
169 | .Fn seed48 . | |
170 | .Pp | |
171 | Finally, | |
172 | .Fn lcong48 | |
173 | allows full control over the multiplicand and addend used in | |
174 | .Fn drand48 , | |
175 | .Fn erand48 , | |
176 | .Fn lrand48 , | |
177 | .Fn nrand48 , | |
178 | .Fn mrand48 , | |
179 | and | |
180 | .Fn jrand48 , | |
181 | and the seed used in | |
182 | .Fn drand48 , | |
183 | .Fn lrand48 , | |
184 | and | |
185 | .Fn mrand48 . | |
9385eb3d | 186 | An array of 7 shorts is passed as argument; the first three shorts are |
5b2abdfb A |
187 | used to initialize the seed; the second three are used to initialize the |
188 | multiplicand; and the last short is used to initialize the addend. | |
189 | It is thus not possible to use values greater than 0xffff as the addend. | |
190 | .Pp | |
191 | Note that all three methods of seeding the random number generator | |
192 | always also set the multiplicand and addend for any of the six | |
193 | generator calls. | |
194 | .Pp | |
195 | For a more powerful random number generator, see | |
196 | .Xr random 3 . | |
5b2abdfb A |
197 | .Sh SEE ALSO |
198 | .Xr rand 3 , | |
199 | .Xr random 3 | |
1f2f436a A |
200 | .Sh AUTHORS |
201 | .An Martin Birgmeier |