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