Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4349 | Serge | 1 | /* |
2 | * Copyright (c) 1993 Martin Birgmeier |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * You may redistribute unmodified or modified versions of this source |
||
6 | * code provided that the above copyright notice and this and the |
||
7 | * following conditions are retained. |
||
8 | * |
||
9 | * This software is provided ``as is'', and comes with no warranties |
||
10 | * of any kind. I shall in no event be liable for anything that happens |
||
11 | * to anyone/anything when using this software. |
||
12 | */ |
||
13 | |||
14 | /* |
||
15 | FUNCTION |
||
16 | < |
||
17 | |||
18 | INDEX |
||
19 | rand48 |
||
20 | INDEX |
||
21 | drand48 |
||
22 | INDEX |
||
23 | erand48 |
||
24 | INDEX |
||
25 | lrand48 |
||
26 | INDEX |
||
27 | nrand48 |
||
28 | INDEX |
||
29 | mrand48 |
||
30 | INDEX |
||
31 | jrand48 |
||
32 | INDEX |
||
33 | srand48 |
||
34 | INDEX |
||
35 | seed48 |
||
36 | INDEX |
||
37 | lcong48 |
||
38 | |||
39 | ANSI_SYNOPSIS |
||
40 | #include |
||
41 | double drand48(void); |
||
42 | double erand48(unsigned short <[xseed]>[3]); |
||
43 | long lrand48(void); |
||
44 | long nrand48(unsigned short <[xseed]>[3]); |
||
45 | long mrand48(void); |
||
46 | long jrand48(unsigned short <[xseed]>[3]); |
||
47 | void srand48(long <[seed]>); |
||
48 | unsigned short *seed48(unsigned short <[xseed]>[3]); |
||
49 | void lcong48(unsigned short <[p]>[7]); |
||
50 | |||
51 | TRAD_SYNOPSIS |
||
52 | #include |
||
53 | double drand48(); |
||
54 | |||
55 | double erand48(<[xseed]>) |
||
56 | unsigned short <[xseed]>[3]; |
||
57 | |||
58 | long lrand48(); |
||
59 | |||
60 | long nrand48(<[xseed]>) |
||
61 | unsigned short <[xseed]>[3]; |
||
62 | |||
63 | long mrand48(); |
||
64 | |||
65 | long jrand48(<[xseed]>) |
||
66 | unsigned short <[xseed]>[3]; |
||
67 | |||
68 | void srand48(<[seed]>) |
||
69 | long <[seed]>; |
||
70 | |||
71 | unsigned short *seed48(<[xseed]>) |
||
72 | unsigned short <[xseed]>[3]; |
||
73 | |||
74 | void lcong48(<[p]>) |
||
75 | unsigned short <[p]>[7]; |
||
76 | |||
77 | DESCRIPTION |
||
78 | The < |
||
79 | using a linear congruential algorithm working on integers 48 bits in size. |
||
80 | The particular formula employed is |
||
81 | r(n+1) = (a * r(n) + c) mod m |
||
82 | where the default values are |
||
83 | for the multiplicand a = 0xfdeece66d = 25214903917 and |
||
84 | the addend c = 0xb = 11. The modulo is always fixed at m = 2 ** 48. |
||
85 | r(n) is called the seed of the random number generator. |
||
86 | |||
87 | For all the six generator routines described next, the first |
||
88 | computational step is to perform a single iteration of the algorithm. |
||
89 | |||
90 | < |
||
91 | return values of type double. 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 | |||
95 | < |
||
96 | return values of type long in the range |
||
97 | [0, 2**31-1]. The high-order (31) bits of |
||
98 | r(n+1) are loaded into the lower bits of the returned value, with |
||
99 | the topmost (sign) bit set to zero. |
||
100 | |||
101 | < |
||
102 | return values of type long in the range |
||
103 | [-2**31, 2**31-1]. The high-order (32) bits of |
||
104 | r(n+1) are loaded into the returned value. |
||
105 | |||
106 | < |
||
107 | use an internal buffer to store r(n). For these functions |
||
108 | the initial value of r(0) = 0x1234abcd330e = 20017429951246. |
||
109 | |||
110 | On the other hand, < |
||
111 | use a user-supplied buffer to store the seed r(n), |
||
112 | which consists of an array of 3 shorts, where the zeroth member |
||
113 | holds the least significant bits. |
||
114 | |||
115 | All functions share the same multiplicand and addend. |
||
116 | |||
117 | < |
||
118 | < |
||
119 | such that the 32 bits of the seed value are copied into the upper 32 bits |
||
120 | of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e. |
||
121 | Additionally, the constant multiplicand and addend of the algorithm are |
||
122 | reset to the default values given above. |
||
123 | |||
124 | < |
||
125 | < |
||
126 | but here all 48 bits of the seed can be specified in an array of 3 shorts, |
||
127 | where the zeroth member specifies the lowest bits. Again, |
||
128 | the constant multiplicand and addend of the algorithm are |
||
129 | reset to the default values given above. |
||
130 | < |
||
131 | the old seed. |
||
132 | This array is statically allocated, thus its contents are lost after |
||
133 | each new call to < |
||
134 | |||
135 | Finally, < |
||
136 | addend used in < |
||
137 | < |
||
138 | and the seed used in < |
||
139 | An array of 7 shorts is passed as parameter; the first three shorts are |
||
140 | used to initialize the seed; the second three are used to initialize the |
||
141 | multiplicand; and the last short is used to initialize the addend. |
||
142 | It is thus not possible to use values greater than 0xffff as the addend. |
||
143 | |||
144 | Note that all three methods of seeding the random number generator |
||
145 | always also set the multiplicand and addend for any of the six |
||
146 | generator calls. |
||
147 | |||
148 | For a more powerful random number generator, see < |
||
149 | |||
150 | PORTABILITY |
||
151 | SUS requires these functions. |
||
152 | |||
153 | No supporting OS subroutines are required. |
||
154 | */ |
||
155 | |||
156 | #include "rand48.h" |
||
157 | |||
158 | void |
||
159 | _DEFUN (__dorand48, (r, xseed), |
||
160 | struct _reent *r _AND |
||
161 | unsigned short xseed[3]) |
||
162 | { |
||
163 | unsigned long accu; |
||
164 | unsigned short temp[2]; |
||
165 | |||
166 | _REENT_CHECK_RAND48(r); |
||
167 | accu = (unsigned long) __rand48_mult[0] * (unsigned long) xseed[0] + |
||
168 | (unsigned long) __rand48_add; |
||
169 | temp[0] = (unsigned short) accu; /* lower 16 bits */ |
||
170 | accu >>= sizeof(unsigned short) * 8; |
||
171 | accu += (unsigned long) __rand48_mult[0] * (unsigned long) xseed[1] + |
||
172 | (unsigned long) __rand48_mult[1] * (unsigned long) xseed[0]; |
||
173 | temp[1] = (unsigned short) accu; /* middle 16 bits */ |
||
174 | accu >>= sizeof(unsigned short) * 8; |
||
175 | accu += __rand48_mult[0] * xseed[2] + __rand48_mult[1] * xseed[1] + __rand48_mult[2] * xseed[0]; |
||
176 | xseed[0] = temp[0]; |
||
177 | xseed[1] = temp[1]; |
||
178 | xseed[2] = (unsigned short) accu; |
||
179 | } |