1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
/* |
20 | 20 |
* This file contains the reimplemented version of the Mersenne Twister |
21 | 21 |
* Generator of Matsumoto and Nishimura. |
22 | 22 |
* |
23 | 23 |
* See the appropriate copyright notice below. |
24 | 24 |
* |
25 | 25 |
* Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, |
26 | 26 |
* All rights reserved. |
27 | 27 |
* |
28 | 28 |
* Redistribution and use in source and binary forms, with or without |
29 | 29 |
* modification, are permitted provided that the following conditions |
30 | 30 |
* are met: |
31 | 31 |
* |
32 | 32 |
* 1. Redistributions of source code must retain the above copyright |
33 | 33 |
* notice, this list of conditions and the following disclaimer. |
34 | 34 |
* |
35 | 35 |
* 2. Redistributions in binary form must reproduce the above copyright |
36 | 36 |
* notice, this list of conditions and the following disclaimer in the |
37 | 37 |
* documentation and/or other materials provided with the distribution. |
38 | 38 |
* |
39 | 39 |
* 3. The names of its contributors may not be used to endorse or promote |
40 | 40 |
* products derived from this software without specific prior written |
41 | 41 |
* permission. |
42 | 42 |
* |
43 | 43 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
44 | 44 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
45 | 45 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
46 | 46 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
47 | 47 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
48 | 48 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
49 | 49 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
50 | 50 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
51 | 51 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
52 | 52 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | 53 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
54 | 54 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
55 | 55 |
* |
56 | 56 |
* |
57 | 57 |
* Any feedback is very welcome. |
58 | 58 |
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html |
59 | 59 |
* email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) |
60 | 60 |
*/ |
61 | 61 |
|
62 | 62 |
#ifndef LEMON_RANDOM_H |
63 | 63 |
#define LEMON_RANDOM_H |
64 | 64 |
|
65 | 65 |
#include <algorithm> |
66 | 66 |
#include <iterator> |
67 | 67 |
#include <vector> |
68 | 68 |
#include <limits> |
69 | 69 |
|
70 | 70 |
#include <lemon/math.h> |
71 | 71 |
#include <lemon/dim2.h> |
72 | 72 |
|
73 | 73 |
///\ingroup misc |
74 | 74 |
///\file |
75 | 75 |
///\brief Mersenne Twister random number generator |
76 | 76 |
|
77 | 77 |
namespace lemon { |
78 | 78 |
|
79 | 79 |
namespace _random_bits { |
80 | 80 |
|
81 | 81 |
template <typename _Word, int _bits = std::numeric_limits<_Word>::digits> |
82 | 82 |
struct RandomTraits {}; |
83 | 83 |
|
84 | 84 |
template <typename _Word> |
85 | 85 |
struct RandomTraits<_Word, 32> { |
86 | 86 |
|
87 | 87 |
typedef _Word Word; |
88 | 88 |
static const int bits = 32; |
89 | 89 |
|
90 | 90 |
static const int length = 624; |
91 | 91 |
static const int shift = 397; |
92 | 92 |
|
93 | 93 |
static const Word mul = 0x6c078965u; |
94 | 94 |
static const Word arrayInit = 0x012BD6AAu; |
95 | 95 |
static const Word arrayMul1 = 0x0019660Du; |
96 | 96 |
static const Word arrayMul2 = 0x5D588B65u; |
97 | 97 |
|
98 | 98 |
static const Word mask = 0x9908B0DFu; |
99 | 99 |
static const Word loMask = (1u << 31) - 1; |
100 | 100 |
static const Word hiMask = ~loMask; |
101 | 101 |
|
102 | 102 |
|
103 | 103 |
static Word tempering(Word rnd) { |
104 | 104 |
rnd ^= (rnd >> 11); |
105 | 105 |
rnd ^= (rnd << 7) & 0x9D2C5680u; |
106 | 106 |
rnd ^= (rnd << 15) & 0xEFC60000u; |
107 | 107 |
rnd ^= (rnd >> 18); |
108 | 108 |
return rnd; |
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
template <typename _Word> |
114 | 114 |
struct RandomTraits<_Word, 64> { |
115 | 115 |
|
116 | 116 |
typedef _Word Word; |
117 | 117 |
static const int bits = 64; |
118 | 118 |
|
119 | 119 |
static const int length = 312; |
120 | 120 |
static const int shift = 156; |
121 | 121 |
|
122 | 122 |
static const Word mul = Word(0x5851F42Du) << 32 | Word(0x4C957F2Du); |
123 | 123 |
static const Word arrayInit = Word(0x00000000u) << 32 |Word(0x012BD6AAu); |
124 | 124 |
static const Word arrayMul1 = Word(0x369DEA0Fu) << 32 |Word(0x31A53F85u); |
125 | 125 |
static const Word arrayMul2 = Word(0x27BB2EE6u) << 32 |Word(0x87B0B0FDu); |
126 | 126 |
|
127 | 127 |
static const Word mask = Word(0xB5026F5Au) << 32 | Word(0xA96619E9u); |
128 | 128 |
static const Word loMask = (Word(1u) << 31) - 1; |
129 | 129 |
static const Word hiMask = ~loMask; |
130 | 130 |
|
131 | 131 |
static Word tempering(Word rnd) { |
132 | 132 |
rnd ^= (rnd >> 29) & (Word(0x55555555u) << 32 | Word(0x55555555u)); |
133 | 133 |
rnd ^= (rnd << 17) & (Word(0x71D67FFFu) << 32 | Word(0xEDA60000u)); |
134 | 134 |
rnd ^= (rnd << 37) & (Word(0xFFF7EEE0u) << 32 | Word(0x00000000u)); |
135 | 135 |
rnd ^= (rnd >> 43); |
136 | 136 |
return rnd; |
137 | 137 |
} |
138 | 138 |
|
139 | 139 |
}; |
140 | 140 |
|
141 | 141 |
template <typename _Word> |
142 | 142 |
class RandomCore { |
143 | 143 |
public: |
144 | 144 |
|
145 | 145 |
typedef _Word Word; |
146 | 146 |
|
147 | 147 |
private: |
148 | 148 |
|
149 | 149 |
static const int bits = RandomTraits<Word>::bits; |
150 | 150 |
|
151 | 151 |
static const int length = RandomTraits<Word>::length; |
152 | 152 |
static const int shift = RandomTraits<Word>::shift; |
153 | 153 |
|
154 | 154 |
public: |
155 | 155 |
|
156 | 156 |
void initState() { |
157 | 157 |
static const Word seedArray[4] = { |
158 | 158 |
0x12345u, 0x23456u, 0x34567u, 0x45678u |
159 | 159 |
}; |
160 | 160 |
|
161 | 161 |
initState(seedArray, seedArray + 4); |
162 | 162 |
} |
163 | 163 |
|
164 | 164 |
void initState(Word seed) { |
165 | 165 |
|
166 | 166 |
static const Word mul = RandomTraits<Word>::mul; |
167 | 167 |
|
168 | 168 |
current = state; |
169 | 169 |
|
170 | 170 |
Word *curr = state + length - 1; |
171 | 171 |
curr[0] = seed; --curr; |
172 | 172 |
for (int i = 1; i < length; ++i) { |
173 | 173 |
curr[0] = (mul * ( curr[1] ^ (curr[1] >> (bits - 2)) ) + i); |
174 | 174 |
--curr; |
175 | 175 |
} |
176 | 176 |
} |
177 | 177 |
|
178 | 178 |
template <typename Iterator> |
179 | 179 |
void initState(Iterator begin, Iterator end) { |
180 | 180 |
|
181 | 181 |
static const Word init = RandomTraits<Word>::arrayInit; |
182 | 182 |
static const Word mul1 = RandomTraits<Word>::arrayMul1; |
183 | 183 |
static const Word mul2 = RandomTraits<Word>::arrayMul2; |
184 | 184 |
|
185 | 185 |
|
186 | 186 |
Word *curr = state + length - 1; --curr; |
187 | 187 |
Iterator it = begin; int cnt = 0; |
188 | 188 |
int num; |
189 | 189 |
|
190 | 190 |
initState(init); |
191 | 191 |
|
192 | 192 |
num = length > end - begin ? length : end - begin; |
193 | 193 |
while (num--) { |
194 | 194 |
curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul1)) |
195 | 195 |
+ *it + cnt; |
196 | 196 |
++it; ++cnt; |
197 | 197 |
if (it == end) { |
198 | 198 |
it = begin; cnt = 0; |
199 | 199 |
} |
200 | 200 |
if (curr == state) { |
201 | 201 |
curr = state + length - 1; curr[0] = state[0]; |
202 | 202 |
} |
203 | 203 |
--curr; |
204 | 204 |
} |
205 | 205 |
|
206 | 206 |
num = length - 1; cnt = length - (curr - state) - 1; |
207 | 207 |
while (num--) { |
208 | 208 |
curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul2)) |
209 | 209 |
- cnt; |
210 | 210 |
--curr; ++cnt; |
211 | 211 |
if (curr == state) { |
212 | 212 |
curr = state + length - 1; curr[0] = state[0]; --curr; |
213 | 213 |
cnt = 1; |
214 | 214 |
} |
215 | 215 |
} |
216 | 216 |
|
217 | 217 |
state[length - 1] = Word(1) << (bits - 1); |
218 | 218 |
} |
219 | 219 |
|
220 | 220 |
void copyState(const RandomCore& other) { |
221 | 221 |
std::copy(other.state, other.state + length, state); |
222 | 222 |
current = state + (other.current - other.state); |
223 | 223 |
} |
224 | 224 |
|
225 | 225 |
Word operator()() { |
226 | 226 |
if (current == state) fillState(); |
227 | 227 |
--current; |
228 | 228 |
Word rnd = *current; |
229 | 229 |
return RandomTraits<Word>::tempering(rnd); |
230 | 230 |
} |
231 | 231 |
|
232 | 232 |
private: |
233 | 233 |
|
234 | 234 |
|
235 | 235 |
void fillState() { |
236 | 236 |
static const Word mask[2] = { 0x0ul, RandomTraits<Word>::mask }; |
237 | 237 |
static const Word loMask = RandomTraits<Word>::loMask; |
238 | 238 |
static const Word hiMask = RandomTraits<Word>::hiMask; |
239 | 239 |
|
240 | 240 |
current = state + length; |
241 | 241 |
|
242 | 242 |
register Word *curr = state + length - 1; |
243 | 243 |
register long num; |
244 | 244 |
|
245 | 245 |
num = length - shift; |
246 | 246 |
while (num--) { |
247 | 247 |
curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^ |
248 | 248 |
curr[- shift] ^ mask[curr[-1] & 1ul]; |
249 | 249 |
--curr; |
250 | 250 |
} |
251 | 251 |
num = shift - 1; |
252 | 252 |
while (num--) { |
253 | 253 |
curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^ |
254 | 254 |
curr[length - shift] ^ mask[curr[-1] & 1ul]; |
255 | 255 |
--curr; |
256 | 256 |
} |
257 | 257 |
state[0] = (((state[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^ |
258 | 258 |
curr[length - shift] ^ mask[curr[length - 1] & 1ul]; |
259 | 259 |
|
260 | 260 |
} |
261 | 261 |
|
262 | 262 |
|
263 | 263 |
Word *current; |
264 | 264 |
Word state[length]; |
265 | 265 |
|
266 | 266 |
}; |
267 | 267 |
|
268 | 268 |
|
269 | 269 |
template <typename Result, |
270 | 270 |
int shift = (std::numeric_limits<Result>::digits + 1) / 2> |
271 | 271 |
struct Masker { |
272 | 272 |
static Result mask(const Result& result) { |
273 | 273 |
return Masker<Result, (shift + 1) / 2>:: |
274 | 274 |
mask(static_cast<Result>(result | (result >> shift))); |
275 | 275 |
} |
276 | 276 |
}; |
277 | 277 |
|
278 | 278 |
template <typename Result> |
279 | 279 |
struct Masker<Result, 1> { |
280 | 280 |
static Result mask(const Result& result) { |
281 | 281 |
return static_cast<Result>(result | (result >> 1)); |
282 | 282 |
} |
283 | 283 |
}; |
284 | 284 |
|
285 | 285 |
template <typename Result, typename Word, |
286 | 286 |
int rest = std::numeric_limits<Result>::digits, int shift = 0, |
287 | 287 |
bool last = rest <= std::numeric_limits<Word>::digits> |
288 | 288 |
struct IntConversion { |
289 | 289 |
static const int bits = std::numeric_limits<Word>::digits; |
290 | 290 |
|
291 | 291 |
static Result convert(RandomCore<Word>& rnd) { |
292 | 292 |
return static_cast<Result>(rnd() >> (bits - rest)) << shift; |
293 | 293 |
} |
294 | 294 |
|
295 | 295 |
}; |
296 | 296 |
|
297 | 297 |
template <typename Result, typename Word, int rest, int shift> |
298 | 298 |
struct IntConversion<Result, Word, rest, shift, false> { |
299 | 299 |
static const int bits = std::numeric_limits<Word>::digits; |
300 | 300 |
|
301 | 301 |
static Result convert(RandomCore<Word>& rnd) { |
302 | 302 |
return (static_cast<Result>(rnd()) << shift) | |
303 | 303 |
IntConversion<Result, Word, rest - bits, shift + bits>::convert(rnd); |
304 | 304 |
} |
305 | 305 |
}; |
306 | 306 |
|
307 | 307 |
|
308 | 308 |
template <typename Result, typename Word, |
309 | 309 |
bool one_word = (std::numeric_limits<Word>::digits < |
310 | 310 |
std::numeric_limits<Result>::digits) > |
311 | 311 |
struct Mapping { |
312 | 312 |
static Result map(RandomCore<Word>& rnd, const Result& bound) { |
313 | 313 |
Word max = Word(bound - 1); |
314 | 314 |
Result mask = Masker<Result>::mask(bound - 1); |
315 | 315 |
Result num; |
316 | 316 |
do { |
317 | 317 |
num = IntConversion<Result, Word>::convert(rnd) & mask; |
318 | 318 |
} while (num > max); |
319 | 319 |
return num; |
320 | 320 |
} |
321 | 321 |
}; |
322 | 322 |
|
323 | 323 |
template <typename Result, typename Word> |
324 | 324 |
struct Mapping<Result, Word, false> { |
325 | 325 |
static Result map(RandomCore<Word>& rnd, const Result& bound) { |
326 | 326 |
Word max = Word(bound - 1); |
327 | 327 |
Word mask = Masker<Word, (std::numeric_limits<Result>::digits + 1) / 2> |
328 | 328 |
::mask(max); |
329 | 329 |
Word num; |
330 | 330 |
do { |
331 | 331 |
num = rnd() & mask; |
332 | 332 |
} while (num > max); |
333 | 333 |
return num; |
334 | 334 |
} |
335 | 335 |
}; |
336 | 336 |
|
337 | 337 |
template <typename Result, int exp, bool pos = (exp >= 0)> |
338 | 338 |
struct ShiftMultiplier { |
339 | 339 |
static const Result multiplier() { |
340 | 340 |
Result res = ShiftMultiplier<Result, exp / 2>::multiplier(); |
341 | 341 |
res *= res; |
342 | 342 |
if ((exp & 1) == 1) res *= static_cast<Result>(2.0); |
343 | 343 |
return res; |
344 | 344 |
} |
345 | 345 |
}; |
346 | 346 |
|
347 | 347 |
template <typename Result, int exp> |
348 | 348 |
struct ShiftMultiplier<Result, exp, false> { |
349 | 349 |
static const Result multiplier() { |
350 | 350 |
Result res = ShiftMultiplier<Result, exp / 2>::multiplier(); |
351 | 351 |
res *= res; |
352 | 352 |
if ((exp & 1) == 1) res *= static_cast<Result>(0.5); |
353 | 353 |
return res; |
354 | 354 |
} |
355 | 355 |
}; |
356 | 356 |
|
357 | 357 |
template <typename Result> |
358 | 358 |
struct ShiftMultiplier<Result, 0, true> { |
359 | 359 |
static const Result multiplier() { |
360 | 360 |
return static_cast<Result>(1.0); |
361 | 361 |
} |
362 | 362 |
}; |
363 | 363 |
|
364 | 364 |
template <typename Result> |
365 | 365 |
struct ShiftMultiplier<Result, -20, true> { |
366 | 366 |
static const Result multiplier() { |
367 | 367 |
return static_cast<Result>(1.0/1048576.0); |
368 | 368 |
} |
369 | 369 |
}; |
370 | 370 |
|
371 | 371 |
template <typename Result> |
372 | 372 |
struct ShiftMultiplier<Result, -32, true> { |
373 | 373 |
static const Result multiplier() { |
374 | 374 |
return static_cast<Result>(1.0/424967296.0); |
375 | 375 |
} |
376 | 376 |
}; |
377 | 377 |
|
378 | 378 |
template <typename Result> |
379 | 379 |
struct ShiftMultiplier<Result, -53, true> { |
380 | 380 |
static const Result multiplier() { |
381 | 381 |
return static_cast<Result>(1.0/9007199254740992.0); |
382 | 382 |
} |
383 | 383 |
}; |
384 | 384 |
|
385 | 385 |
template <typename Result> |
386 | 386 |
struct ShiftMultiplier<Result, -64, true> { |
387 | 387 |
static const Result multiplier() { |
388 | 388 |
return static_cast<Result>(1.0/18446744073709551616.0); |
389 | 389 |
} |
390 | 390 |
}; |
391 | 391 |
|
392 | 392 |
template <typename Result, int exp> |
393 | 393 |
struct Shifting { |
394 | 394 |
static Result shift(const Result& result) { |
395 | 395 |
return result * ShiftMultiplier<Result, exp>::multiplier(); |
396 | 396 |
} |
397 | 397 |
}; |
398 | 398 |
|
399 | 399 |
template <typename Result, typename Word, |
400 | 400 |
int rest = std::numeric_limits<Result>::digits, int shift = 0, |
401 | 401 |
bool last = rest <= std::numeric_limits<Word>::digits> |
402 | 402 |
struct RealConversion{ |
403 | 403 |
static const int bits = std::numeric_limits<Word>::digits; |
404 | 404 |
|
405 | 405 |
static Result convert(RandomCore<Word>& rnd) { |
406 | 406 |
return Shifting<Result, - shift - rest>:: |
407 | 407 |
shift(static_cast<Result>(rnd() >> (bits - rest))); |
408 | 408 |
} |
409 | 409 |
}; |
410 | 410 |
|
411 | 411 |
template <typename Result, typename Word, int rest, int shift> |
412 | 412 |
struct RealConversion<Result, Word, rest, shift, false> { |
413 | 413 |
static const int bits = std::numeric_limits<Word>::digits; |
414 | 414 |
|
415 | 415 |
static Result convert(RandomCore<Word>& rnd) { |
416 | 416 |
return Shifting<Result, - shift - bits>:: |
417 | 417 |
shift(static_cast<Result>(rnd())) + |
418 | 418 |
RealConversion<Result, Word, rest-bits, shift + bits>:: |
419 | 419 |
convert(rnd); |
420 | 420 |
} |
421 | 421 |
}; |
422 | 422 |
|
423 | 423 |
template <typename Result, typename Word> |
424 | 424 |
struct Initializer { |
425 | 425 |
|
426 | 426 |
template <typename Iterator> |
427 | 427 |
static void init(RandomCore<Word>& rnd, Iterator begin, Iterator end) { |
428 | 428 |
std::vector<Word> ws; |
429 | 429 |
for (Iterator it = begin; it != end; ++it) { |
430 | 430 |
ws.push_back(Word(*it)); |
431 | 431 |
} |
432 | 432 |
rnd.initState(ws.begin(), ws.end()); |
433 | 433 |
} |
434 | 434 |
|
435 | 435 |
static void init(RandomCore<Word>& rnd, Result seed) { |
436 | 436 |
rnd.initState(seed); |
437 | 437 |
} |
438 | 438 |
}; |
439 | 439 |
|
440 | 440 |
template <typename Word> |
441 | 441 |
struct BoolConversion { |
442 | 442 |
static bool convert(RandomCore<Word>& rnd) { |
443 | 443 |
return (rnd() & 1) == 1; |
444 | 444 |
} |
445 | 445 |
}; |
446 | 446 |
|
447 | 447 |
template <typename Word> |
448 | 448 |
struct BoolProducer { |
449 | 449 |
Word buffer; |
450 | 450 |
int num; |
451 | 451 |
|
452 | 452 |
BoolProducer() : num(0) {} |
453 | 453 |
|
454 | 454 |
bool convert(RandomCore<Word>& rnd) { |
455 | 455 |
if (num == 0) { |
456 | 456 |
buffer = rnd(); |
457 | 457 |
num = RandomTraits<Word>::bits; |
458 | 458 |
} |
459 | 459 |
bool r = (buffer & 1); |
460 | 460 |
buffer >>= 1; |
461 | 461 |
--num; |
462 | 462 |
return r; |
463 | 463 |
} |
464 | 464 |
}; |
465 | 465 |
|
466 | 466 |
} |
467 | 467 |
|
468 | 468 |
/// \ingroup misc |
469 | 469 |
/// |
470 | 470 |
/// \brief Mersenne Twister random number generator |
471 | 471 |
/// |
472 | 472 |
/// The Mersenne Twister is a twisted generalized feedback |
473 | 473 |
/// shift-register generator of Matsumoto and Nishimura. The period |
474 | 474 |
/// of this generator is \f$ 2^{19937} - 1 \f$ and it is |
475 | 475 |
/// equi-distributed in 623 dimensions for 32-bit numbers. The time |
476 | 476 |
/// performance of this generator is comparable to the commonly used |
477 | 477 |
/// generators. |
478 | 478 |
/// |
479 | 479 |
/// This implementation is specialized for both 32-bit and 64-bit |
480 | 480 |
/// architectures. The generators differ sligthly in the |
481 | 481 |
/// initialization and generation phase so they produce two |
482 | 482 |
/// completly different sequences. |
483 | 483 |
/// |
484 | 484 |
/// The generator gives back random numbers of serveral types. To |
485 | 485 |
/// get a random number from a range of a floating point type you |
486 | 486 |
/// can use one form of the \c operator() or the \c real() member |
487 | 487 |
/// function. If you want to get random number from the {0, 1, ..., |
488 | 488 |
/// n-1} integer range use the \c operator[] or the \c integer() |
489 | 489 |
/// method. And to get random number from the whole range of an |
490 | 490 |
/// integer type you can use the argumentless \c integer() or \c |
491 | 491 |
/// uinteger() functions. After all you can get random bool with |
492 | 492 |
/// equal chance of true and false or given probability of true |
493 | 493 |
/// result with the \c boolean() member functions. |
494 | 494 |
/// |
495 | 495 |
///\code |
496 | 496 |
/// // The commented code is identical to the other |
497 | 497 |
/// double a = rnd(); // [0.0, 1.0) |
498 | 498 |
/// // double a = rnd.real(); // [0.0, 1.0) |
499 | 499 |
/// double b = rnd(100.0); // [0.0, 100.0) |
500 | 500 |
/// // double b = rnd.real(100.0); // [0.0, 100.0) |
501 | 501 |
/// double c = rnd(1.0, 2.0); // [1.0, 2.0) |
502 | 502 |
/// // double c = rnd.real(1.0, 2.0); // [1.0, 2.0) |
503 | 503 |
/// int d = rnd[100000]; // 0..99999 |
504 | 504 |
/// // int d = rnd.integer(100000); // 0..99999 |
505 | 505 |
/// int e = rnd[6] + 1; // 1..6 |
506 | 506 |
/// // int e = rnd.integer(1, 1 + 6); // 1..6 |
507 | 507 |
/// int b = rnd.uinteger<int>(); // 0 .. 2^31 - 1 |
508 | 508 |
/// int c = rnd.integer<int>(); // - 2^31 .. 2^31 - 1 |
509 | 509 |
/// bool g = rnd.boolean(); // P(g = true) = 0.5 |
510 | 510 |
/// bool h = rnd.boolean(0.8); // P(h = true) = 0.8 |
511 | 511 |
///\endcode |
512 | 512 |
/// |
513 | 513 |
/// LEMON provides a global instance of the random number |
514 | 514 |
/// generator which name is \ref lemon::rnd "rnd". Usually it is a |
515 | 515 |
/// good programming convenience to use this global generator to get |
516 | 516 |
/// random numbers. |
517 | 517 |
class Random { |
518 | 518 |
private: |
519 | 519 |
|
520 | 520 |
// Architecture word |
521 | 521 |
typedef unsigned long Word; |
522 | 522 |
|
523 | 523 |
_random_bits::RandomCore<Word> core; |
524 | 524 |
_random_bits::BoolProducer<Word> bool_producer; |
525 | 525 |
|
526 | 526 |
|
527 | 527 |
public: |
528 | 528 |
|
529 | 529 |
/// \brief Default constructor |
530 | 530 |
/// |
531 | 531 |
/// Constructor with constant seeding. |
532 | 532 |
Random() { core.initState(); } |
533 | 533 |
|
534 | 534 |
/// \brief Constructor with seed |
535 | 535 |
/// |
536 | 536 |
/// Constructor with seed. The current number type will be converted |
537 | 537 |
/// to the architecture word type. |
538 | 538 |
template <typename Number> |
539 | 539 |
Random(Number seed) { |
540 | 540 |
_random_bits::Initializer<Number, Word>::init(core, seed); |
541 | 541 |
} |
542 | 542 |
|
543 | 543 |
/// \brief Constructor with array seeding |
544 | 544 |
/// |
545 | 545 |
/// Constructor with array seeding. The given range should contain |
546 | 546 |
/// any number type and the numbers will be converted to the |
547 | 547 |
/// architecture word type. |
548 | 548 |
template <typename Iterator> |
549 | 549 |
Random(Iterator begin, Iterator end) { |
550 | 550 |
typedef typename std::iterator_traits<Iterator>::value_type Number; |
551 | 551 |
_random_bits::Initializer<Number, Word>::init(core, begin, end); |
552 | 552 |
} |
553 | 553 |
|
554 | 554 |
/// \brief Copy constructor |
555 | 555 |
/// |
556 | 556 |
/// Copy constructor. The generated sequence will be identical to |
557 | 557 |
/// the other sequence. It can be used to save the current state |
558 | 558 |
/// of the generator and later use it to generate the same |
559 | 559 |
/// sequence. |
560 | 560 |
Random(const Random& other) { |
561 | 561 |
core.copyState(other.core); |
562 | 562 |
} |
563 | 563 |
|
564 | 564 |
/// \brief Assign operator |
565 | 565 |
/// |
566 | 566 |
/// Assign operator. The generated sequence will be identical to |
567 | 567 |
/// the other sequence. It can be used to save the current state |
568 | 568 |
/// of the generator and later use it to generate the same |
569 | 569 |
/// sequence. |
570 | 570 |
Random& operator=(const Random& other) { |
571 | 571 |
if (&other != this) { |
572 | 572 |
core.copyState(other.core); |
573 | 573 |
} |
574 | 574 |
return *this; |
575 | 575 |
} |
576 | 576 |
|
577 | 577 |
/// \brief Seeding random sequence |
578 | 578 |
/// |
579 | 579 |
/// Seeding the random sequence. The current number type will be |
580 | 580 |
/// converted to the architecture word type. |
581 | 581 |
template <typename Number> |
582 | 582 |
void seed(Number seed) { |
583 | 583 |
_random_bits::Initializer<Number, Word>::init(core, seed); |
584 | 584 |
} |
585 | 585 |
|
586 | 586 |
/// \brief Seeding random sequence |
587 | 587 |
/// |
588 | 588 |
/// Seeding the random sequence. The given range should contain |
589 | 589 |
/// any number type and the numbers will be converted to the |
590 | 590 |
/// architecture word type. |
591 | 591 |
template <typename Iterator> |
592 | 592 |
void seed(Iterator begin, Iterator end) { |
593 | 593 |
typedef typename std::iterator_traits<Iterator>::value_type Number; |
594 | 594 |
_random_bits::Initializer<Number, Word>::init(core, begin, end); |
595 | 595 |
} |
596 | 596 |
|
597 | 597 |
/// \brief Returns a random real number from the range [0, 1) |
598 | 598 |
/// |
599 | 599 |
/// It returns a random real number from the range [0, 1). The |
600 | 600 |
/// default Number type is \c double. |
601 | 601 |
template <typename Number> |
602 | 602 |
Number real() { |
603 | 603 |
return _random_bits::RealConversion<Number, Word>::convert(core); |
604 | 604 |
} |
605 | 605 |
|
606 | 606 |
double real() { |
607 | 607 |
return real<double>(); |
608 | 608 |
} |
609 | 609 |
|
610 | 610 |
/// \brief Returns a random real number the range [0, b) |
611 | 611 |
/// |
612 | 612 |
/// It returns a random real number from the range [0, b). |
613 | 613 |
template <typename Number> |
614 | 614 |
Number real(Number b) { |
615 | 615 |
return real<Number>() * b; |
616 | 616 |
} |
617 | 617 |
|
618 | 618 |
/// \brief Returns a random real number from the range [a, b) |
619 | 619 |
/// |
620 | 620 |
/// It returns a random real number from the range [a, b). |
621 | 621 |
template <typename Number> |
622 | 622 |
Number real(Number a, Number b) { |
623 | 623 |
return real<Number>() * (b - a) + a; |
624 | 624 |
} |
625 | 625 |
|
626 | 626 |
/// \brief Returns a random real number from the range [0, 1) |
627 | 627 |
/// |
628 | 628 |
/// It returns a random double from the range [0, 1). |
629 | 629 |
double operator()() { |
630 | 630 |
return real<double>(); |
631 | 631 |
} |
632 | 632 |
|
633 | 633 |
/// \brief Returns a random real number from the range [0, b) |
634 | 634 |
/// |
635 | 635 |
/// It returns a random real number from the range [0, b). |
636 | 636 |
template <typename Number> |
637 | 637 |
Number operator()(Number b) { |
638 | 638 |
return real<Number>() * b; |
639 | 639 |
} |
640 | 640 |
|
641 | 641 |
/// \brief Returns a random real number from the range [a, b) |
642 | 642 |
/// |
643 | 643 |
/// It returns a random real number from the range [a, b). |
644 | 644 |
template <typename Number> |
645 | 645 |
Number operator()(Number a, Number b) { |
646 | 646 |
return real<Number>() * (b - a) + a; |
647 | 647 |
} |
648 | 648 |
|
649 | 649 |
/// \brief Returns a random integer from a range |
650 | 650 |
/// |
651 | 651 |
/// It returns a random integer from the range {0, 1, ..., b - 1}. |
652 | 652 |
template <typename Number> |
653 | 653 |
Number integer(Number b) { |
654 | 654 |
return _random_bits::Mapping<Number, Word>::map(core, b); |
655 | 655 |
} |
656 | 656 |
|
657 | 657 |
/// \brief Returns a random integer from a range |
658 | 658 |
/// |
659 | 659 |
/// It returns a random integer from the range {a, a + 1, ..., b - 1}. |
660 | 660 |
template <typename Number> |
661 | 661 |
Number integer(Number a, Number b) { |
662 | 662 |
return _random_bits::Mapping<Number, Word>::map(core, b - a) + a; |
663 | 663 |
} |
664 | 664 |
|
665 | 665 |
/// \brief Returns a random integer from a range |
666 | 666 |
/// |
667 | 667 |
/// It returns a random integer from the range {0, 1, ..., b - 1}. |
668 | 668 |
template <typename Number> |
669 | 669 |
Number operator[](Number b) { |
670 | 670 |
return _random_bits::Mapping<Number, Word>::map(core, b); |
671 | 671 |
} |
672 | 672 |
|
673 | 673 |
/// \brief Returns a random non-negative integer |
674 | 674 |
/// |
675 | 675 |
/// It returns a random non-negative integer uniformly from the |
676 | 676 |
/// whole range of the current \c Number type. The default result |
677 | 677 |
/// type of this function is <tt>unsigned int</tt>. |
678 | 678 |
template <typename Number> |
679 | 679 |
Number uinteger() { |
680 | 680 |
return _random_bits::IntConversion<Number, Word>::convert(core); |
681 | 681 |
} |
682 | 682 |
|
683 | 683 |
unsigned int uinteger() { |
684 | 684 |
return uinteger<unsigned int>(); |
685 | 685 |
} |
686 | 686 |
|
687 | 687 |
/// \brief Returns a random integer |
688 | 688 |
/// |
689 | 689 |
/// It returns a random integer uniformly from the whole range of |
690 | 690 |
/// the current \c Number type. The default result type of this |
691 | 691 |
/// function is \c int. |
692 | 692 |
template <typename Number> |
693 | 693 |
Number integer() { |
694 | 694 |
static const int nb = std::numeric_limits<Number>::digits + |
695 | 695 |
(std::numeric_limits<Number>::is_signed ? 1 : 0); |
696 | 696 |
return _random_bits::IntConversion<Number, Word, nb>::convert(core); |
697 | 697 |
} |
698 | 698 |
|
699 | 699 |
int integer() { |
700 | 700 |
return integer<int>(); |
701 | 701 |
} |
702 | 702 |
|
703 | 703 |
/// \brief Returns a random bool |
704 | 704 |
/// |
705 | 705 |
/// It returns a random bool. The generator holds a buffer for |
706 | 706 |
/// random bits. Every time when it become empty the generator makes |
707 | 707 |
/// a new random word and fill the buffer up. |
708 | 708 |
bool boolean() { |
709 | 709 |
return bool_producer.convert(core); |
710 | 710 |
} |
711 | 711 |
|
712 | 712 |
///\name Non-uniform distributions |
713 | 713 |
/// |
714 | 714 |
|
715 | 715 |
///@{ |
716 | 716 |
|
717 | 717 |
/// \brief Returns a random bool |
718 | 718 |
/// |
719 | 719 |
/// It returns a random bool with given probability of true result. |
720 | 720 |
bool boolean(double p) { |
721 | 721 |
return operator()() < p; |
722 | 722 |
} |
723 | 723 |
|
724 | 724 |
/// Standard Gauss distribution |
725 | 725 |
|
726 | 726 |
/// Standard Gauss distribution. |
727 | 727 |
/// \note The Cartesian form of the Box-Muller |
728 | 728 |
/// transformation is used to generate a random normal distribution. |
729 | 729 |
/// \todo Consider using the "ziggurat" method instead. |
730 | 730 |
double gauss() |
731 | 731 |
{ |
732 | 732 |
double V1,V2,S; |
733 | 733 |
do { |
734 | 734 |
V1=2*real<double>()-1; |
735 | 735 |
V2=2*real<double>()-1; |
736 | 736 |
S=V1*V1+V2*V2; |
737 | 737 |
} while(S>=1); |
738 | 738 |
return std::sqrt(-2*std::log(S)/S)*V1; |
739 | 739 |
} |
740 | 740 |
/// Gauss distribution with given mean and standard deviation |
741 | 741 |
|
742 | 742 |
/// Gauss distribution with given mean and standard deviation. |
743 | 743 |
/// \sa gauss() |
744 | 744 |
double gauss(double mean,double std_dev) |
745 | 745 |
{ |
746 | 746 |
return gauss()*std_dev+mean; |
747 | 747 |
} |
748 | 748 |
|
749 | 749 |
/// Exponential distribution with given mean |
750 | 750 |
|
751 | 751 |
/// This function generates an exponential distribution random number |
752 | 752 |
/// with mean <tt>1/lambda</tt>. |
753 | 753 |
/// |
754 | 754 |
double exponential(double lambda=1.0) |
755 | 755 |
{ |
756 | 756 |
return -std::log(1.0-real<double>())/lambda; |
757 | 757 |
} |
758 | 758 |
|
759 | 759 |
/// Gamma distribution with given integer shape |
760 | 760 |
|
761 | 761 |
/// This function generates a gamma distribution random number. |
762 | 762 |
/// |
763 | 763 |
///\param k shape parameter (<tt>k>0</tt> integer) |
764 | 764 |
double gamma(int k) |
765 | 765 |
{ |
766 | 766 |
double s = 0; |
767 | 767 |
for(int i=0;i<k;i++) s-=std::log(1.0-real<double>()); |
768 | 768 |
return s; |
769 | 769 |
} |
770 | 770 |
|
771 | 771 |
/// Gamma distribution with given shape and scale parameter |
772 | 772 |
|
773 | 773 |
/// This function generates a gamma distribution random number. |
774 | 774 |
/// |
775 | 775 |
///\param k shape parameter (<tt>k>0</tt>) |
776 | 776 |
///\param theta scale parameter |
777 | 777 |
/// |
778 | 778 |
double gamma(double k,double theta=1.0) |
779 | 779 |
{ |
780 | 780 |
double xi,nu; |
781 | 781 |
const double delta = k-std::floor(k); |
782 | 782 |
const double v0=E/(E-delta); |
783 | 783 |
do { |
784 | 784 |
double V0=1.0-real<double>(); |
785 | 785 |
double V1=1.0-real<double>(); |
786 | 786 |
double V2=1.0-real<double>(); |
787 | 787 |
if(V2<=v0) |
788 | 788 |
{ |
789 | 789 |
xi=std::pow(V1,1.0/delta); |
790 | 790 |
nu=V0*std::pow(xi,delta-1.0); |
791 | 791 |
} |
792 | 792 |
else |
793 | 793 |
{ |
794 | 794 |
xi=1.0-std::log(V1); |
795 | 795 |
nu=V0*std::exp(-xi); |
796 | 796 |
} |
797 | 797 |
} while(nu>std::pow(xi,delta-1.0)*std::exp(-xi)); |
798 |
return theta*(xi |
|
798 |
return theta*(xi+gamma(int(std::floor(k)))); |
|
799 | 799 |
} |
800 | 800 |
|
801 | 801 |
/// Weibull distribution |
802 | 802 |
|
803 | 803 |
/// This function generates a Weibull distribution random number. |
804 | 804 |
/// |
805 | 805 |
///\param k shape parameter (<tt>k>0</tt>) |
806 | 806 |
///\param lambda scale parameter (<tt>lambda>0</tt>) |
807 | 807 |
/// |
808 | 808 |
double weibull(double k,double lambda) |
809 | 809 |
{ |
810 | 810 |
return lambda*pow(-std::log(1.0-real<double>()),1.0/k); |
811 | 811 |
} |
812 | 812 |
|
813 | 813 |
/// Pareto distribution |
814 | 814 |
|
815 | 815 |
/// This function generates a Pareto distribution random number. |
816 | 816 |
/// |
817 | 817 |
///\param k shape parameter (<tt>k>0</tt>) |
818 | 818 |
///\param x_min location parameter (<tt>x_min>0</tt>) |
819 | 819 |
/// |
820 | 820 |
double pareto(double k,double x_min) |
821 | 821 |
{ |
822 |
return exponential(gamma(k,1.0/x_min)); |
|
822 |
return exponential(gamma(k,1.0/x_min))+x_min; |
|
823 | 823 |
} |
824 | 824 |
|
825 | 825 |
/// Poisson distribution |
826 | 826 |
|
827 | 827 |
/// This function generates a Poisson distribution random number with |
828 | 828 |
/// parameter \c lambda. |
829 | 829 |
/// |
830 | 830 |
/// The probability mass function of this distribusion is |
831 | 831 |
/// \f[ \frac{e^{-\lambda}\lambda^k}{k!} \f] |
832 | 832 |
/// \note The algorithm is taken from the book of Donald E. Knuth titled |
833 | 833 |
/// ''Seminumerical Algorithms'' (1969). Its running time is linear in the |
834 | 834 |
/// return value. |
835 | 835 |
|
836 | 836 |
int poisson(double lambda) |
837 | 837 |
{ |
838 | 838 |
const double l = std::exp(-lambda); |
839 | 839 |
int k=0; |
840 | 840 |
double p = 1.0; |
841 | 841 |
do { |
842 | 842 |
k++; |
843 | 843 |
p*=real<double>(); |
844 | 844 |
} while (p>=l); |
845 | 845 |
return k-1; |
846 | 846 |
} |
847 | 847 |
|
848 | 848 |
///@} |
849 | 849 |
|
850 | 850 |
///\name Two dimensional distributions |
851 | 851 |
/// |
852 | 852 |
|
853 | 853 |
///@{ |
854 | 854 |
|
855 | 855 |
/// Uniform distribution on the full unit circle |
856 | 856 |
|
857 | 857 |
/// Uniform distribution on the full unit circle. |
858 | 858 |
/// |
859 | 859 |
dim2::Point<double> disc() |
860 | 860 |
{ |
861 | 861 |
double V1,V2; |
862 | 862 |
do { |
863 | 863 |
V1=2*real<double>()-1; |
864 | 864 |
V2=2*real<double>()-1; |
865 | 865 |
|
866 | 866 |
} while(V1*V1+V2*V2>=1); |
867 | 867 |
return dim2::Point<double>(V1,V2); |
868 | 868 |
} |
869 | 869 |
/// A kind of two dimensional Gauss distribution |
870 | 870 |
|
871 | 871 |
/// This function provides a turning symmetric two-dimensional distribution. |
872 | 872 |
/// Both coordinates are of standard normal distribution, but they are not |
873 | 873 |
/// independent. |
874 | 874 |
/// |
875 | 875 |
/// \note The coordinates are the two random variables provided by |
876 | 876 |
/// the Box-Muller method. |
877 | 877 |
dim2::Point<double> gauss2() |
878 | 878 |
{ |
879 | 879 |
double V1,V2,S; |
880 | 880 |
do { |
881 | 881 |
V1=2*real<double>()-1; |
882 | 882 |
V2=2*real<double>()-1; |
883 | 883 |
S=V1*V1+V2*V2; |
884 | 884 |
} while(S>=1); |
885 | 885 |
double W=std::sqrt(-2*std::log(S)/S); |
886 | 886 |
return dim2::Point<double>(W*V1,W*V2); |
887 | 887 |
} |
888 | 888 |
/// A kind of two dimensional exponential distribution |
889 | 889 |
|
890 | 890 |
/// This function provides a turning symmetric two-dimensional distribution. |
891 | 891 |
/// The x-coordinate is of conditionally exponential distribution |
892 | 892 |
/// with the condition that x is positive and y=0. If x is negative and |
893 | 893 |
/// y=0 then, -x is of exponential distribution. The same is true for the |
894 | 894 |
/// y-coordinate. |
895 | 895 |
dim2::Point<double> exponential2() |
896 | 896 |
{ |
897 | 897 |
double V1,V2,S; |
898 | 898 |
do { |
899 | 899 |
V1=2*real<double>()-1; |
900 | 900 |
V2=2*real<double>()-1; |
901 | 901 |
S=V1*V1+V2*V2; |
902 | 902 |
} while(S>=1); |
903 | 903 |
double W=-std::log(S)/S; |
904 | 904 |
return dim2::Point<double>(W*V1,W*V2); |
905 | 905 |
} |
906 | 906 |
|
907 | 907 |
///@} |
908 | 908 |
}; |
909 | 909 |
|
910 | 910 |
|
911 | 911 |
extern Random rnd; |
912 | 912 |
|
913 | 913 |
} |
914 | 914 |
|
915 | 915 |
#endif |
0 comments (0 inline)