1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/random.h Mon Oct 02 16:11:00 2006 +0000
1.3 @@ -0,0 +1,511 @@
1.4 +/* -*- C++ -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library
1.7 + *
1.8 + * Copyright (C) 2003-2006
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_RANDOM_H
1.23 +#define LEMON_RANDOM_H
1.24 +
1.25 +#include <algorithm>
1.26 +
1.27 +#include <ctime>
1.28 +#include <cmath>
1.29 +#include <cmath>
1.30 +
1.31 +///\ingroup misc
1.32 +///\file
1.33 +///\brief Mersenne Twister random number generator
1.34 +///
1.35 +///\author Balazs Dezso
1.36 +
1.37 +namespace lemon {
1.38 +
1.39 +#if WORD_BIT == 32
1.40 +
1.41 + /// \ingroup misc
1.42 + ///
1.43 + /// \brief Mersenne Twister random number generator
1.44 + ///
1.45 + /// The Mersenne Twister is a twisted generalized feedback
1.46 + /// shift-register generator of Matsumoto and Nishimura. The period of
1.47 + /// this generator is \f$ 2^{19937} - 1 \f$ and it is equi-distributed in
1.48 + /// 623 dimensions. The time performance of this generator is comparable
1.49 + /// to the commonly used generators.
1.50 + ///
1.51 + /// \author Balazs Dezso
1.52 + class Random {
1.53 +
1.54 + static const int length = 624;
1.55 + static const int shift = 397;
1.56 +
1.57 + public:
1.58 +
1.59 + static const unsigned long min = 0ul;
1.60 + static const unsigned long max = ~0ul;
1.61 +
1.62 + /// \brief Constructor
1.63 + ///
1.64 + /// Constructor with time dependent seeding.
1.65 + Random() { initState(std::time(0)); }
1.66 +
1.67 + /// \brief Constructor
1.68 + ///
1.69 + /// Constructor
1.70 + Random(unsigned long seed) { initState(seed); }
1.71 +
1.72 + /// \brief Copy constructor
1.73 + ///
1.74 + /// Copy constructor. The generated sequence will be identical to
1.75 + /// the other sequence.
1.76 + Random(const Random& other) {
1.77 + std::copy(other.state, other.state + length, state);
1.78 + current = state + (other.current - other.state);
1.79 + }
1.80 +
1.81 + /// \brief Assign operator
1.82 + ///
1.83 + /// Assign operator. The generated sequence will be identical to
1.84 + /// the other sequence.
1.85 + Random& operator=(const Random& other) {
1.86 + if (&other != this) {
1.87 + std::copy(other.state, other.state + length, state);
1.88 + current = state + (other.current - other.state);
1.89 + }
1.90 + return *this;
1.91 + }
1.92 +
1.93 + /// \brief Returns an unsigned random number
1.94 + ///
1.95 + /// It returns an unsigned integer random number from the range
1.96 + /// \f$ \{0, 1, \dots, 2^{32} - 1\} \f$.
1.97 + unsigned long getUnsigned() {
1.98 + if (current == state) fillState();
1.99 + --current;
1.100 + unsigned long rnd = *current;
1.101 + rnd ^= (rnd >> 11);
1.102 + rnd ^= (rnd << 7) & 0x9D2C5680ul;
1.103 + rnd ^= (rnd << 15) & 0xEFC60000ul;
1.104 + rnd ^= (rnd >> 18);
1.105 + return rnd;
1.106 + }
1.107 +
1.108 + /// \brief Returns a random number
1.109 + ///
1.110 + /// It returns an integer random number from the range
1.111 + /// \f$ \{-2^{31}, \dots, 2^{31} - 1\} \f$.
1.112 + long getInt() {
1.113 + return (long)getUnsigned();
1.114 + }
1.115 +
1.116 + /// \brief Returns an unsigned integer random number
1.117 + ///
1.118 + /// It returns an unsigned integer random number from the range
1.119 + /// \f$ \{0, 1, \dots, 2^{31} - 1\} \f$.
1.120 + long getNatural() {
1.121 + return (long)(getUnsigned() >> 1);
1.122 + }
1.123 +
1.124 + /// \brief Returns a random bool
1.125 + ///
1.126 + /// It returns a random bool.
1.127 + bool getBool() {
1.128 + return (bool)(getUnsigned() & 1);
1.129 + }
1.130 +
1.131 + /// \brief Returns a real random number
1.132 + ///
1.133 + /// It returns a real random number from the range
1.134 + /// \f$ [0, 1) \f$. The double will have 32 significant bits.
1.135 + double getReal() {
1.136 + return std::ldexp((double)getUnsigned(), -32);
1.137 + }
1.138 +
1.139 + /// \brief Returns a real random number
1.140 + ///
1.141 + /// It returns a real random number from the range
1.142 + /// \f$ [0, 1) \f$. The double will have 53 significant bits,
1.143 + /// but it is slower than the \c getReal().
1.144 + double getPrecReal() {
1.145 + return std::ldexp((double)(getUnsigned() >> 5), -27) +
1.146 + std::ldexp((double)(getUnsigned() >> 6), -53);
1.147 + }
1.148 +
1.149 + /// \brief Returns an unsigned random number from a given range
1.150 + ///
1.151 + /// It returns an unsigned integer random number from the range
1.152 + /// \f$ \{0, 1, \dots, n - 1\} \f$.
1.153 + unsigned long getUnsigned(unsigned long n) {
1.154 + unsigned long mask = n - 1, rnd;
1.155 + mask |= mask >> 1;
1.156 + mask |= mask >> 2;
1.157 + mask |= mask >> 4;
1.158 + mask |= mask >> 8;
1.159 + mask |= mask >> 16;
1.160 + do rnd = getUnsigned() & mask; while (rnd >= n);
1.161 + return rnd;
1.162 + }
1.163 +
1.164 + /// \brief Returns a random number from a given range
1.165 + ///
1.166 + /// It returns an unsigned integer random number from the range
1.167 + /// \f$ \{0, 1, \dots, n - 1\} \f$.
1.168 + long getInt(long n) {
1.169 + long mask = n - 1, rnd;
1.170 + mask |= mask >> 1;
1.171 + mask |= mask >> 2;
1.172 + mask |= mask >> 4;
1.173 + mask |= mask >> 8;
1.174 + mask |= mask >> 16;
1.175 + do rnd = getUnsigned() & mask; while (rnd >= n);
1.176 + return rnd;
1.177 + }
1.178 +
1.179 + private:
1.180 +
1.181 + void initState(unsigned long seed) {
1.182 + static const unsigned long mul = 0x6c078965ul;
1.183 +
1.184 + current = state;
1.185 +
1.186 + unsigned long *curr = state + length - 1;
1.187 + curr[0] = seed; --curr;
1.188 + for (int i = 1; i < length; ++i) {
1.189 + curr[0] = (mul * ( curr[1] ^ (curr[1] >> 30) ) + i);
1.190 + --curr;
1.191 + }
1.192 + }
1.193 +
1.194 + void fillState() {
1.195 + static const unsigned long mask[2] = { 0x0ul, 0x9908B0DFul };
1.196 + static const unsigned long loMask = (1ul << 31) - 1;
1.197 + static const unsigned long hiMask = ~loMask;
1.198 +
1.199 + current = state + length;
1.200 +
1.201 + register unsigned long *curr = state + length - 1;
1.202 + register long num;
1.203 +
1.204 + num = length - shift;
1.205 + while (num--) {
1.206 + curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
1.207 + curr[- shift] ^ mask[curr[-1] & 1ul];
1.208 + --curr;
1.209 + }
1.210 + num = shift - 1;
1.211 + while (num--) {
1.212 + curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
1.213 + curr[length - shift] ^ mask[curr[-1] & 1ul];
1.214 + --curr;
1.215 + }
1.216 + curr[0] = (((curr[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^
1.217 + curr[length - shift] ^ mask[curr[length - 1] & 1ul];
1.218 +
1.219 + }
1.220 +
1.221 + unsigned long *current;
1.222 + unsigned long state[length];
1.223 +
1.224 + };
1.225 +
1.226 +#elif WORD_BIT == 64
1.227 +
1.228 + /// \brief Mersenne Twister random number generator
1.229 + ///
1.230 + /// The Mersenne Twister is a twisted generalized feedback
1.231 + /// shift-register generator of Matsumoto and Nishimura. The period of
1.232 + /// this generator is \f$ 2^{19937} - 1 \f$ and it is equi-distributed in
1.233 + /// 311 dimensions. The time performance of this generator is comparable
1.234 + /// to the commonly used generators.
1.235 + class Random {
1.236 +
1.237 + static const int length = 312;
1.238 + static const int shift = 156;
1.239 +
1.240 + public:
1.241 +
1.242 + static const unsigned long min = 0ul;
1.243 + static const unsigned long max = ~0ul;
1.244 +
1.245 + /// \brief Constructor
1.246 + ///
1.247 + /// Constructor with time dependent seeding.
1.248 + Random() { initState(std::time(0)); }
1.249 +
1.250 + /// \brief Constructor
1.251 + ///
1.252 + /// Constructor
1.253 + Random(unsigned long seed) { initState(seed); }
1.254 +
1.255 + /// \brief Copy constructor
1.256 + ///
1.257 + /// Copy constructor. The generated sequence will be identical to
1.258 + /// the other sequence.
1.259 + Random(const Random& other) {
1.260 + std::copy(other.state, other.state + length, state);
1.261 + current = state + (other.current - other.state);
1.262 + }
1.263 +
1.264 + /// \brief Assign operator
1.265 + ///
1.266 + /// Assign operator. The generated sequence will be identical to
1.267 + /// the other sequence.
1.268 + Random& operator=(const Random& other) {
1.269 + if (&other != this) {
1.270 + std::copy(other.state, other.state + length, state);
1.271 + current = state + (other.current - other.state);
1.272 + }
1.273 + return *this;
1.274 + }
1.275 +
1.276 + /// \brief Returns an unsigned random number
1.277 + ///
1.278 + /// It returns an unsigned integer random number from the range
1.279 + /// \f$ \{0, 1, \dots, 2^{64} - 1\} \f$.
1.280 + unsigned long getUnsigned() {
1.281 + if (current == state) fillState();
1.282 + --current;
1.283 + unsigned long rnd = *current;
1.284 + rnd ^= (rnd >> 29) & 0x5555555555555555ul;
1.285 + rnd ^= (rnd << 17) & 0x71D67FFFEDA60000ul;
1.286 + rnd ^= (rnd << 37) & 0xFFF7EEE000000000ul;
1.287 + rnd ^= (rnd >> 43);
1.288 + return rnd;
1.289 + }
1.290 +
1.291 + /// \brief Returns a random number
1.292 + ///
1.293 + /// It returns an integer random number from the range
1.294 + /// \f$ \{-2^{63}, \dots, 2^{63} - 1\} \f$.
1.295 + long getInt() {
1.296 + return (long)getUnsigned();
1.297 + }
1.298 +
1.299 + /// \brief Returns an unsigned integer random number
1.300 + ///
1.301 + /// It returns an unsigned integer random number from the range
1.302 + /// \f$ \{0, 1, \dots, 2^{63} - 1\} \f$.
1.303 + long getNatural() {
1.304 + return (long)(getUnsigned() >> 1);
1.305 + }
1.306 +
1.307 + /// \brief Returns a random bool
1.308 + ///
1.309 + /// It returns a random bool.
1.310 + bool getBool() {
1.311 + return (bool)(getUnsigned() & 1);
1.312 + }
1.313 +
1.314 + /// \brief Returns a real random number
1.315 + ///
1.316 + /// It returns a real random number from the range
1.317 + /// \f$ [0, 1) \f$.
1.318 + double getReal() {
1.319 + return std::ldexp((double)(getUnsigned() >> 11), -53);
1.320 + }
1.321 +
1.322 + /// \brief Returns a real random number
1.323 + ///
1.324 + /// It returns a real random number from the range
1.325 + /// \f$ [0, 1) \f$. This function is identical to the \c getReal().
1.326 + double getPrecReal() {
1.327 + return getReal();
1.328 + }
1.329 +
1.330 + /// \brief Returns an unsigned random number from a given range
1.331 + ///
1.332 + /// It returns an unsigned integer random number from the range
1.333 + /// \f$ \{0, 1, \dots, n - 1\} \f$.
1.334 + unsigned long getUnsigned(unsigned long n) {
1.335 + unsigned long mask = n - 1, rnd;
1.336 + mask |= mask >> 1;
1.337 + mask |= mask >> 2;
1.338 + mask |= mask >> 4;
1.339 + mask |= mask >> 8;
1.340 + mask |= mask >> 16;
1.341 + mask |= mask >> 32;
1.342 + do rnd = getUnsigned() & mask; while (rnd >= n);
1.343 + return rnd;
1.344 + }
1.345 +
1.346 + /// \brief Returns a random number from a given range
1.347 + ///
1.348 + /// It returns an unsigned integer random number from the range
1.349 + /// \f$ \{0, 1, \dots, n - 1\} \f$.
1.350 + long getInt(long n) {
1.351 + long mask = n - 1, rnd;
1.352 + mask |= mask >> 1;
1.353 + mask |= mask >> 2;
1.354 + mask |= mask >> 4;
1.355 + mask |= mask >> 8;
1.356 + mask |= mask >> 16;
1.357 + mask |= mask >> 32;
1.358 + do rnd = getUnsigned() & mask; while (rnd >= n);
1.359 + return rnd;
1.360 + }
1.361 +
1.362 + private:
1.363 +
1.364 + void initState(unsigned long seed) {
1.365 +
1.366 + static const unsigned long mul = 0x5851F42D4C957F2Dul;
1.367 +
1.368 + current = state;
1.369 +
1.370 + unsigned long *curr = state + length - 1;
1.371 + curr[0] = seed; --curr;
1.372 + for (int i = 1; i < length; ++i) {
1.373 + curr[0] = (mul * ( curr[1] ^ (curr[1] >> 62) ) + i);
1.374 + --curr;
1.375 + }
1.376 + }
1.377 +
1.378 + void fillState() {
1.379 + static const unsigned long mask[2] = { 0x0ul, 0xB5026F5AA96619E9ul };
1.380 + static const unsigned long loMask = (1ul << 31) - 1;
1.381 + static const unsigned long hiMask = ~loMask;
1.382 +
1.383 + current = state + length;
1.384 +
1.385 + register unsigned long *curr = state + length - 1;
1.386 + register int num;
1.387 +
1.388 + num = length - shift;
1.389 + while (num--) {
1.390 + curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
1.391 + curr[- shift] ^ mask[curr[-1] & 1ul];
1.392 + --curr;
1.393 + }
1.394 + num = shift - 1;
1.395 + while (num--) {
1.396 + curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
1.397 + curr[length - shift] ^ mask[curr[-1] & 1ul];
1.398 + --curr;
1.399 + }
1.400 + curr[0] = (((curr[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^
1.401 + curr[length - shift] ^ mask[curr[length - 1] & 1ul];
1.402 +
1.403 + }
1.404 +
1.405 + unsigned long *current;
1.406 + unsigned long state[length];
1.407 +
1.408 + };
1.409 +
1.410 +#else
1.411 +
1.412 + /// \brief Mersenne Twister random number generator
1.413 + ///
1.414 + /// The Mersenne Twister is a twisted generalized feedback
1.415 + /// shift-register generator of Matsumoto and Nishimura. There is
1.416 + /// two different implementation in the Lemon library, one for
1.417 + /// 32-bit processors and one for 64-bit processors. The period of
1.418 + /// the generated sequence is \f$ 2^{19937} - 1 \f$, the generated
1.419 + /// sequence of 32-bit random numbers is equi-distributed in 623
1.420 + /// dimensions. The time performance of this generator is comparable
1.421 + /// to the commonly used generators.
1.422 + class Random {
1.423 + public:
1.424 +
1.425 + static const unsigned long min = 0ul;
1.426 + static const unsigned long max = ~0ul;
1.427 +
1.428 + /// \brief Constructor
1.429 + ///
1.430 + /// Constructor with time dependent seeding.
1.431 + Random() {}
1.432 +
1.433 + /// \brief Constructor
1.434 + ///
1.435 + /// Constructor
1.436 + Random(unsigned long seed) {}
1.437 +
1.438 + /// \brief Copy constructor
1.439 + ///
1.440 + /// Copy constructor. The generated sequence will be identical to
1.441 + /// the other sequence.
1.442 + Random(const Random& other) {}
1.443 +
1.444 + /// \brief Assign operator
1.445 + ///
1.446 + /// Assign operator. The generated sequence will be identical to
1.447 + /// the other sequence.
1.448 + Random& operator=(const Random& other) { return *this; }
1.449 +
1.450 + /// \brief Returns an unsigned random number
1.451 + ///
1.452 + /// It returns an unsigned integer random number from the range
1.453 + /// \f$ \{0, 1, \dots, 2^{64} - 1\} \f$ for 64-bit processors and from
1.454 + /// \f$ \{0, 1, \dots, 2^{32} - 1\} \f$ for 32-bit processors.
1.455 + unsigned long getUnsigned() { return 0ul; }
1.456 +
1.457 + /// \brief Returns a random number
1.458 + ///
1.459 + /// It returns an integer random number from the range
1.460 + /// \f$ \{-2^{63}, \dots, 2^{63} - 1\} \f$ for 64-bit processors and from
1.461 + /// \f$ \{-2^{31}, \dots, 2^{31} - 1\} \f$ for 32-bit processors.
1.462 + long getInt() { return 0l; }
1.463 +
1.464 + /// \brief Returns an unsigned integer random number
1.465 + ///
1.466 + /// It returns an unsigned integer random number from the range
1.467 + /// \f$ \{0, 1, \dots, 2^{63} - 1\} \f$ for 64-bit processors and
1.468 + /// from \f$ \{0, 1, \dots, 2^{31} - 1\} \f$ for 32-bit processors.
1.469 + long getNatural() { return 0l; }
1.470 +
1.471 + /// \brief Returns a random bool
1.472 + ///
1.473 + /// It returns a random bool.
1.474 + bool getBool() { return false; }
1.475 +
1.476 + /// \brief Returns a real random number
1.477 + ///
1.478 + /// It returns a real random number from the range
1.479 + /// \f$ [0, 1) \f$. For 32-bit processors the generated random
1.480 + /// number will just have 32 significant bits.
1.481 + double getReal() { return 0.0; }
1.482 +
1.483 + /// \brief Returns a real random number
1.484 + ///
1.485 + /// It returns a real random number from the range
1.486 + /// \f$ [0, 1) \f$. This function returns random numbers with 53
1.487 + /// significant bits for 32-bit processors. For 64-bit processors
1.488 + /// it is identical to the \c getReal().
1.489 + double getPrecReal() { return 0.0; }
1.490 +
1.491 + /// \brief Returns an unsigned random number from a given range
1.492 + ///
1.493 + /// It returns an unsigned integer random number from the range
1.494 + /// \f$ \{0, 1, \dots, n - 1\} \f$.
1.495 + unsigned long getUnsigned(unsigned long n) { return 0; }
1.496 +
1.497 + /// \brief Returns a random number from a given range
1.498 + ///
1.499 + /// It returns an unsigned integer random number from the range
1.500 + /// \f$ \{0, 1, \dots, n - 1\} \f$.
1.501 + long getInt(long n) { return 0; }
1.502 +
1.503 + };
1.504 +
1.505 +#endif
1.506 +
1.507 + /// \brief Global random number generator instance
1.508 + ///
1.509 + /// A global mersenne twister random number generator instance
1.510 + extern Random rnd;
1.511 +
1.512 +}
1.513 +
1.514 +#endif