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