… | |
… | |
194 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
194 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
195 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
195 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
196 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
196 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
197 | struct tausworthe_random_generator |
197 | struct tausworthe_random_generator |
198 | { |
198 | { |
|
|
199 | // generator |
199 | uint32_t state [4]; |
200 | uint32_t state [4]; |
200 | |
201 | |
201 | tausworthe_random_generator (uint32_t seed); |
202 | void operator =(const tausworthe_random_generator &src) |
|
|
203 | { |
|
|
204 | state [0] = src.state [0]; |
|
|
205 | state [1] = src.state [1]; |
|
|
206 | state [2] = src.state [2]; |
|
|
207 | state [3] = src.state [3]; |
|
|
208 | } |
|
|
209 | |
|
|
210 | void seed (uint32_t seed); |
202 | uint32_t next (); |
211 | uint32_t next (); |
203 | |
212 | |
|
|
213 | // uniform distribution |
204 | uint32_t operator ()(uint32_t r_max) |
214 | uint32_t operator ()(uint32_t r_max) |
205 | { |
215 | { |
206 | return next () % r_max; |
216 | return is_constant (r_max) |
|
|
217 | ? this->next () % r_max |
|
|
218 | : get_range (r_max); |
207 | } |
219 | } |
208 | |
220 | |
209 | // return a number within (min .. max) |
221 | // return a number within (min .. max) |
210 | int operator () (int r_min, int r_max) |
222 | int operator () (int r_min, int r_max) |
211 | { |
223 | { |
|
|
224 | return is_constant (r_min) && is_constant (r_max) |
212 | return r_min + (*this) (max (r_max - r_min + 1, 1)); |
225 | ? r_min + (*this) (max (r_max - r_min + 1, 1)) |
|
|
226 | : get_range (r_min, r_max); |
213 | } |
227 | } |
214 | |
228 | |
215 | double operator ()() |
229 | double operator ()() |
216 | { |
230 | { |
217 | return next () / (double)0xFFFFFFFFU; |
231 | return this->next () / (double)0xFFFFFFFFU; |
218 | } |
232 | } |
|
|
233 | |
|
|
234 | protected: |
|
|
235 | uint32_t get_range (uint32_t r_max); |
|
|
236 | int get_range (int r_min, int r_max); |
219 | }; |
237 | }; |
220 | |
238 | |
221 | typedef tausworthe_random_generator rand_gen; |
239 | typedef tausworthe_random_generator rand_gen; |
222 | |
240 | |
223 | extern rand_gen rndm; |
241 | extern rand_gen rndm; |