lemon/random.h
changeset 10 99e499ca560b
child 11 ea5945b2da9c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/random.h	Fri Dec 21 00:07:03 2007 +0000
     1.3 @@ -0,0 +1,850 @@
     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-2007
     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 +/*
    1.23 + * This file contains the reimplemented version of the Mersenne Twister
    1.24 + * Generator of Matsumoto and Nishimura.
    1.25 + *
    1.26 + * See the appropriate copyright notice below.
    1.27 + * 
    1.28 + * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
    1.29 + * All rights reserved.                          
    1.30 + *
    1.31 + * Redistribution and use in source and binary forms, with or without
    1.32 + * modification, are permitted provided that the following conditions
    1.33 + * are met:
    1.34 + *
    1.35 + * 1. Redistributions of source code must retain the above copyright
    1.36 + *    notice, this list of conditions and the following disclaimer.
    1.37 + *
    1.38 + * 2. Redistributions in binary form must reproduce the above copyright
    1.39 + *    notice, this list of conditions and the following disclaimer in the
    1.40 + *    documentation and/or other materials provided with the distribution.
    1.41 + *
    1.42 + * 3. The names of its contributors may not be used to endorse or promote 
    1.43 + *    products derived from this software without specific prior written 
    1.44 + *    permission.
    1.45 + *
    1.46 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.47 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.48 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    1.49 + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
    1.50 + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.51 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.52 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    1.53 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.54 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.55 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.56 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    1.57 + * OF THE POSSIBILITY OF SUCH DAMAGE.
    1.58 + *
    1.59 + *
    1.60 + * Any feedback is very welcome.
    1.61 + * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
    1.62 + * email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
    1.63 + */
    1.64 +
    1.65 +#ifndef LEMON_RANDOM_H
    1.66 +#define LEMON_RANDOM_H
    1.67 +
    1.68 +#include <algorithm>
    1.69 +#include <iterator>
    1.70 +#include <vector>
    1.71 +
    1.72 +#include <ctime>
    1.73 +#include <cmath>
    1.74 +
    1.75 +#include <lemon/dim2.h>
    1.76 +///\ingroup misc
    1.77 +///\file
    1.78 +///\brief Mersenne Twister random number generator
    1.79 +///
    1.80 +///\author Balazs Dezso
    1.81 +
    1.82 +namespace lemon {
    1.83 +
    1.84 +  namespace _random_bits {
    1.85 +    
    1.86 +    template <typename _Word, int _bits = std::numeric_limits<_Word>::digits>
    1.87 +    struct RandomTraits {};
    1.88 +
    1.89 +    template <typename _Word>
    1.90 +    struct RandomTraits<_Word, 32> {
    1.91 +
    1.92 +      typedef _Word Word;
    1.93 +      static const int bits = 32;
    1.94 +
    1.95 +      static const int length = 624;
    1.96 +      static const int shift = 397;
    1.97 +      
    1.98 +      static const Word mul = 0x6c078965u;
    1.99 +      static const Word arrayInit = 0x012BD6AAu;
   1.100 +      static const Word arrayMul1 = 0x0019660Du;
   1.101 +      static const Word arrayMul2 = 0x5D588B65u;
   1.102 +
   1.103 +      static const Word mask = 0x9908B0DFu;
   1.104 +      static const Word loMask = (1u << 31) - 1;
   1.105 +      static const Word hiMask = ~loMask;
   1.106 +
   1.107 +
   1.108 +      static Word tempering(Word rnd) {
   1.109 +        rnd ^= (rnd >> 11);
   1.110 +        rnd ^= (rnd << 7) & 0x9D2C5680u;
   1.111 +        rnd ^= (rnd << 15) & 0xEFC60000u;
   1.112 +        rnd ^= (rnd >> 18);
   1.113 +        return rnd;
   1.114 +      }
   1.115 +
   1.116 +    };
   1.117 +
   1.118 +    template <typename _Word>
   1.119 +    struct RandomTraits<_Word, 64> {
   1.120 +
   1.121 +      typedef _Word Word;
   1.122 +      static const int bits = 64;
   1.123 +
   1.124 +      static const int length = 312;
   1.125 +      static const int shift = 156;
   1.126 +
   1.127 +      static const Word mul = Word(0x5851F42Du) << 32 | Word(0x4C957F2Du);
   1.128 +      static const Word arrayInit = Word(0x00000000u) << 32 |Word(0x012BD6AAu);
   1.129 +      static const Word arrayMul1 = Word(0x369DEA0Fu) << 32 |Word(0x31A53F85u);
   1.130 +      static const Word arrayMul2 = Word(0x27BB2EE6u) << 32 |Word(0x87B0B0FDu);
   1.131 +
   1.132 +      static const Word mask = Word(0xB5026F5Au) << 32 | Word(0xA96619E9u);
   1.133 +      static const Word loMask = (Word(1u) << 31) - 1;
   1.134 +      static const Word hiMask = ~loMask;
   1.135 +
   1.136 +      static Word tempering(Word rnd) {
   1.137 +        rnd ^= (rnd >> 29) & (Word(0x55555555u) << 32 | Word(0x55555555u));
   1.138 +        rnd ^= (rnd << 17) & (Word(0x71D67FFFu) << 32 | Word(0xEDA60000u));
   1.139 +        rnd ^= (rnd << 37) & (Word(0xFFF7EEE0u) << 32 | Word(0x00000000u));
   1.140 +        rnd ^= (rnd >> 43);
   1.141 +        return rnd;
   1.142 +      }
   1.143 +
   1.144 +    };
   1.145 +
   1.146 +    template <typename _Word>
   1.147 +    class RandomCore {
   1.148 +    public:
   1.149 +
   1.150 +      typedef _Word Word;
   1.151 +
   1.152 +    private:
   1.153 +
   1.154 +      static const int bits = RandomTraits<Word>::bits;
   1.155 +
   1.156 +      static const int length = RandomTraits<Word>::length;
   1.157 +      static const int shift = RandomTraits<Word>::shift;
   1.158 +
   1.159 +    public:
   1.160 +
   1.161 +      void initState() {
   1.162 +        static const Word seedArray[4] = {
   1.163 +          0x12345u, 0x23456u, 0x34567u, 0x45678u
   1.164 +        };
   1.165 +    
   1.166 +        initState(seedArray, seedArray + 4);
   1.167 +      }
   1.168 +
   1.169 +      void initState(Word seed) {
   1.170 +
   1.171 +        static const Word mul = RandomTraits<Word>::mul;
   1.172 +
   1.173 +        current = state; 
   1.174 +
   1.175 +        Word *curr = state + length - 1;
   1.176 +        curr[0] = seed; --curr;
   1.177 +        for (int i = 1; i < length; ++i) {
   1.178 +          curr[0] = (mul * ( curr[1] ^ (curr[1] >> (bits - 2)) ) + i);
   1.179 +          --curr;
   1.180 +        }
   1.181 +      }
   1.182 +
   1.183 +      template <typename Iterator>
   1.184 +      void initState(Iterator begin, Iterator end) {
   1.185 +
   1.186 +        static const Word init = RandomTraits<Word>::arrayInit;
   1.187 +        static const Word mul1 = RandomTraits<Word>::arrayMul1;
   1.188 +        static const Word mul2 = RandomTraits<Word>::arrayMul2;
   1.189 +
   1.190 +
   1.191 +        Word *curr = state + length - 1; --curr;
   1.192 +        Iterator it = begin; int cnt = 0;
   1.193 +        int num;
   1.194 +
   1.195 +        initState(init);
   1.196 +
   1.197 +        num = length > end - begin ? length : end - begin;
   1.198 +        while (num--) {
   1.199 +          curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul1)) 
   1.200 +            + *it + cnt;
   1.201 +          ++it; ++cnt;
   1.202 +          if (it == end) {
   1.203 +            it = begin; cnt = 0;
   1.204 +          }
   1.205 +          if (curr == state) {
   1.206 +            curr = state + length - 1; curr[0] = state[0];
   1.207 +          }
   1.208 +          --curr;
   1.209 +        }
   1.210 +
   1.211 +        num = length - 1; cnt = length - (curr - state) - 1;
   1.212 +        while (num--) {
   1.213 +          curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul2))
   1.214 +            - cnt;
   1.215 +          --curr; ++cnt;
   1.216 +          if (curr == state) {
   1.217 +            curr = state + length - 1; curr[0] = state[0]; --curr;
   1.218 +            cnt = 1;
   1.219 +          }
   1.220 +        }
   1.221 +        
   1.222 +        state[length - 1] = Word(1) << (bits - 1);
   1.223 +      }
   1.224 +      
   1.225 +      void copyState(const RandomCore& other) {
   1.226 +        std::copy(other.state, other.state + length, state);
   1.227 +        current = state + (other.current - other.state);
   1.228 +      }
   1.229 +
   1.230 +      Word operator()() {
   1.231 +        if (current == state) fillState();
   1.232 +        --current;
   1.233 +        Word rnd = *current;
   1.234 +        return RandomTraits<Word>::tempering(rnd);
   1.235 +      }
   1.236 +
   1.237 +    private:
   1.238 +
   1.239 +  
   1.240 +      void fillState() {
   1.241 +        static const Word mask[2] = { 0x0ul, RandomTraits<Word>::mask };
   1.242 +        static const Word loMask = RandomTraits<Word>::loMask;
   1.243 +        static const Word hiMask = RandomTraits<Word>::hiMask;
   1.244 +
   1.245 +        current = state + length; 
   1.246 +
   1.247 +        register Word *curr = state + length - 1;
   1.248 +        register long num;
   1.249 +      
   1.250 +        num = length - shift;
   1.251 +        while (num--) {
   1.252 +          curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
   1.253 +            curr[- shift] ^ mask[curr[-1] & 1ul];
   1.254 +          --curr;
   1.255 +        }
   1.256 +        num = shift - 1;
   1.257 +        while (num--) {
   1.258 +          curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
   1.259 +            curr[length - shift] ^ mask[curr[-1] & 1ul];
   1.260 +          --curr;
   1.261 +        }
   1.262 +        curr[0] = (((curr[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^
   1.263 +          curr[length - shift] ^ mask[curr[length - 1] & 1ul];
   1.264 +
   1.265 +      }
   1.266 +
   1.267 +  
   1.268 +      Word *current;
   1.269 +      Word state[length];
   1.270 +      
   1.271 +    };
   1.272 +
   1.273 +
   1.274 +    template <typename Result, 
   1.275 +              int shift = (std::numeric_limits<Result>::digits + 1) / 2>
   1.276 +    struct Masker {
   1.277 +      static Result mask(const Result& result) {
   1.278 +        return Masker<Result, (shift + 1) / 2>::
   1.279 +          mask(static_cast<Result>(result | (result >> shift)));
   1.280 +      }
   1.281 +    };
   1.282 +    
   1.283 +    template <typename Result>
   1.284 +    struct Masker<Result, 1> {
   1.285 +      static Result mask(const Result& result) {
   1.286 +        return static_cast<Result>(result | (result >> 1));
   1.287 +      }
   1.288 +    };
   1.289 +
   1.290 +    template <typename Result, typename Word, 
   1.291 +              int rest = std::numeric_limits<Result>::digits, int shift = 0, 
   1.292 +              bool last = rest <= std::numeric_limits<Word>::digits>
   1.293 +    struct IntConversion {
   1.294 +      static const int bits = std::numeric_limits<Word>::digits;
   1.295 +    
   1.296 +      static Result convert(RandomCore<Word>& rnd) {
   1.297 +        return static_cast<Result>(rnd() >> (bits - rest)) << shift;
   1.298 +      }
   1.299 +      
   1.300 +    }; 
   1.301 +
   1.302 +    template <typename Result, typename Word, int rest, int shift> 
   1.303 +    struct IntConversion<Result, Word, rest, shift, false> {
   1.304 +      static const int bits = std::numeric_limits<Word>::digits;
   1.305 +
   1.306 +      static Result convert(RandomCore<Word>& rnd) {
   1.307 +        return (static_cast<Result>(rnd()) << shift) | 
   1.308 +          IntConversion<Result, Word, rest - bits, shift + bits>::convert(rnd);
   1.309 +      }
   1.310 +    };
   1.311 +
   1.312 +
   1.313 +    template <typename Result, typename Word,
   1.314 +              bool one_word = (std::numeric_limits<Word>::digits < 
   1.315 +			       std::numeric_limits<Result>::digits) >
   1.316 +    struct Mapping {
   1.317 +      static Result map(RandomCore<Word>& rnd, const Result& bound) {
   1.318 +        Word max = Word(bound - 1);
   1.319 +        Result mask = Masker<Result>::mask(bound - 1);
   1.320 +        Result num;
   1.321 +        do {
   1.322 +          num = IntConversion<Result, Word>::convert(rnd) & mask; 
   1.323 +        } while (num > max);
   1.324 +        return num;
   1.325 +      }
   1.326 +    };
   1.327 +
   1.328 +    template <typename Result, typename Word>
   1.329 +    struct Mapping<Result, Word, false> {
   1.330 +      static Result map(RandomCore<Word>& rnd, const Result& bound) {
   1.331 +        Word max = Word(bound - 1);
   1.332 +        Word mask = Masker<Word, (std::numeric_limits<Result>::digits + 1) / 2>
   1.333 +          ::mask(max);
   1.334 +        Word num;
   1.335 +        do {
   1.336 +          num = rnd() & mask;
   1.337 +        } while (num > max);
   1.338 +        return num;
   1.339 +      }
   1.340 +    };
   1.341 +
   1.342 +    template <typename Result, int exp, bool pos = (exp >= 0)>
   1.343 +    struct ShiftMultiplier {
   1.344 +      static const Result multiplier() {
   1.345 +        Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
   1.346 +        res *= res;
   1.347 +        if ((exp & 1) == 1) res *= static_cast<Result>(2.0);
   1.348 +        return res; 
   1.349 +      }
   1.350 +    };
   1.351 +
   1.352 +    template <typename Result, int exp>
   1.353 +    struct ShiftMultiplier<Result, exp, false> {
   1.354 +      static const Result multiplier() {
   1.355 +        Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
   1.356 +        res *= res;
   1.357 +        if ((exp & 1) == 1) res *= static_cast<Result>(0.5);
   1.358 +        return res; 
   1.359 +      }
   1.360 +    };
   1.361 +
   1.362 +    template <typename Result>
   1.363 +    struct ShiftMultiplier<Result, 0, true> {
   1.364 +      static const Result multiplier() {
   1.365 +        return static_cast<Result>(1.0); 
   1.366 +      }
   1.367 +    };
   1.368 +
   1.369 +    template <typename Result>
   1.370 +    struct ShiftMultiplier<Result, -20, true> {
   1.371 +      static const Result multiplier() {
   1.372 +        return static_cast<Result>(1.0/1048576.0); 
   1.373 +      }
   1.374 +    };
   1.375 +    
   1.376 +    template <typename Result>
   1.377 +    struct ShiftMultiplier<Result, -32, true> {
   1.378 +      static const Result multiplier() {
   1.379 +        return static_cast<Result>(1.0/424967296.0); 
   1.380 +      }
   1.381 +    };
   1.382 +
   1.383 +    template <typename Result>
   1.384 +    struct ShiftMultiplier<Result, -53, true> {
   1.385 +      static const Result multiplier() {
   1.386 +        return static_cast<Result>(1.0/9007199254740992.0); 
   1.387 +      }
   1.388 +    };
   1.389 +
   1.390 +    template <typename Result>
   1.391 +    struct ShiftMultiplier<Result, -64, true> {
   1.392 +      static const Result multiplier() {
   1.393 +        return static_cast<Result>(1.0/18446744073709551616.0); 
   1.394 +      }
   1.395 +    };
   1.396 +
   1.397 +    template <typename Result, int exp>
   1.398 +    struct Shifting {
   1.399 +      static Result shift(const Result& result) {
   1.400 +        return result * ShiftMultiplier<Result, exp>::multiplier();
   1.401 +      }
   1.402 +    };
   1.403 +
   1.404 +    template <typename Result, typename Word,
   1.405 +              int rest = std::numeric_limits<Result>::digits, int shift = 0, 
   1.406 +              bool last = rest <= std::numeric_limits<Word>::digits>
   1.407 +    struct RealConversion{ 
   1.408 +      static const int bits = std::numeric_limits<Word>::digits;
   1.409 +
   1.410 +      static Result convert(RandomCore<Word>& rnd) {
   1.411 +        return Shifting<Result, - shift - rest>::
   1.412 +          shift(static_cast<Result>(rnd() >> (bits - rest)));
   1.413 +      }
   1.414 +    };
   1.415 +
   1.416 +    template <typename Result, typename Word, int rest, int shift>
   1.417 +    struct RealConversion<Result, Word, rest, shift, false> { 
   1.418 +      static const int bits = std::numeric_limits<Word>::digits;
   1.419 +
   1.420 +      static Result convert(RandomCore<Word>& rnd) {
   1.421 +        return Shifting<Result, - shift - bits>::
   1.422 +          shift(static_cast<Result>(rnd())) +
   1.423 +          RealConversion<Result, Word, rest-bits, shift + bits>::
   1.424 +          convert(rnd);
   1.425 +      }
   1.426 +    };
   1.427 +
   1.428 +    template <typename Result, typename Word>
   1.429 +    struct Initializer {
   1.430 +
   1.431 +      template <typename Iterator>
   1.432 +      static void init(RandomCore<Word>& rnd, Iterator begin, Iterator end) {
   1.433 +        std::vector<Word> ws;
   1.434 +        for (Iterator it = begin; it != end; ++it) {
   1.435 +          ws.push_back(Word(*it));
   1.436 +        }
   1.437 +        rnd.initState(ws.begin(), ws.end());
   1.438 +      }
   1.439 +
   1.440 +      static void init(RandomCore<Word>& rnd, Result seed) {
   1.441 +        rnd.initState(seed);
   1.442 +      }
   1.443 +    };
   1.444 +
   1.445 +    template <typename Word>
   1.446 +    struct BoolConversion {
   1.447 +      static bool convert(RandomCore<Word>& rnd) {
   1.448 +        return (rnd() & 1) == 1;
   1.449 +      }
   1.450 +    };
   1.451 +
   1.452 +    template <typename Word>
   1.453 +    struct BoolProducer {
   1.454 +      Word buffer;
   1.455 +      int num;
   1.456 +      
   1.457 +      BoolProducer() : num(0) {}
   1.458 +
   1.459 +      bool convert(RandomCore<Word>& rnd) {
   1.460 +        if (num == 0) {
   1.461 +          buffer = rnd();
   1.462 +          num = RandomTraits<Word>::bits;
   1.463 +        }
   1.464 +        bool r = (buffer & 1);
   1.465 +        buffer >>= 1;
   1.466 +        --num;
   1.467 +        return r;
   1.468 +      }
   1.469 +    };
   1.470 +
   1.471 +  }
   1.472 +
   1.473 +  /// \ingroup misc
   1.474 +  ///
   1.475 +  /// \brief Mersenne Twister random number generator
   1.476 +  ///
   1.477 +  /// The Mersenne Twister is a twisted generalized feedback
   1.478 +  /// shift-register generator of Matsumoto and Nishimura. The period
   1.479 +  /// of this generator is \f$ 2^{19937} - 1 \f$ and it is
   1.480 +  /// equi-distributed in 623 dimensions for 32-bit numbers. The time
   1.481 +  /// performance of this generator is comparable to the commonly used
   1.482 +  /// generators.
   1.483 +  ///
   1.484 +  /// This implementation is specialized for both 32-bit and 64-bit
   1.485 +  /// architectures. The generators differ sligthly in the
   1.486 +  /// initialization and generation phase so they produce two
   1.487 +  /// completly different sequences.
   1.488 +  ///
   1.489 +  /// The generator gives back random numbers of serveral types. To
   1.490 +  /// get a random number from a range of a floating point type you
   1.491 +  /// can use one form of the \c operator() or the \c real() member
   1.492 +  /// function. If you want to get random number from the {0, 1, ...,
   1.493 +  /// n-1} integer range use the \c operator[] or the \c integer()
   1.494 +  /// method. And to get random number from the whole range of an
   1.495 +  /// integer type you can use the argumentless \c integer() or \c
   1.496 +  /// uinteger() functions. After all you can get random bool with
   1.497 +  /// equal chance of true and false or given probability of true
   1.498 +  /// result with the \c boolean() member functions.
   1.499 +  ///
   1.500 +  ///\code
   1.501 +  /// // The commented code is identical to the other
   1.502 +  /// double a = rnd();                     // [0.0, 1.0)
   1.503 +  /// // double a = rnd.real();             // [0.0, 1.0)
   1.504 +  /// double b = rnd(100.0);                // [0.0, 100.0)
   1.505 +  /// // double b = rnd.real(100.0);        // [0.0, 100.0)
   1.506 +  /// double c = rnd(1.0, 2.0);             // [1.0, 2.0)
   1.507 +  /// // double c = rnd.real(1.0, 2.0);     // [1.0, 2.0)
   1.508 +  /// int d = rnd[100000];                  // 0..99999
   1.509 +  /// // int d = rnd.integer(100000);       // 0..99999
   1.510 +  /// int e = rnd[6] + 1;                   // 1..6
   1.511 +  /// // int e = rnd.integer(1, 1 + 6);     // 1..6
   1.512 +  /// int b = rnd.uinteger<int>();          // 0 .. 2^31 - 1
   1.513 +  /// int c = rnd.integer<int>();           // - 2^31 .. 2^31 - 1
   1.514 +  /// bool g = rnd.boolean();               // P(g = true) = 0.5
   1.515 +  /// bool h = rnd.boolean(0.8);            // P(h = true) = 0.8
   1.516 +  ///\endcode
   1.517 +  ///
   1.518 +  /// The lemon provides a global instance of the random number
   1.519 +  /// generator which name is \ref lemon::rnd "rnd". Usually it is a
   1.520 +  /// good programming convenience to use this global generator to get
   1.521 +  /// random numbers.
   1.522 +  ///
   1.523 +  /// \author Balazs Dezso
   1.524 +  class Random {
   1.525 +  private:
   1.526 +
   1.527 +    // architecture word
   1.528 +    typedef unsigned long Word;
   1.529 +    
   1.530 +    _random_bits::RandomCore<Word> core;
   1.531 +    _random_bits::BoolProducer<Word> bool_producer;
   1.532 +    
   1.533 +
   1.534 +  public:
   1.535 +
   1.536 +    /// \brief Constructor
   1.537 +    ///
   1.538 +    /// Constructor with constant seeding.
   1.539 +    Random() { core.initState(); }
   1.540 +
   1.541 +    /// \brief Constructor
   1.542 +    ///
   1.543 +    /// Constructor with seed. The current number type will be converted
   1.544 +    /// to the architecture word type.
   1.545 +    template <typename Number>
   1.546 +    Random(Number seed) { 
   1.547 +      _random_bits::Initializer<Number, Word>::init(core, seed);
   1.548 +    }
   1.549 +
   1.550 +    /// \brief Constructor
   1.551 +    ///
   1.552 +    /// Constructor with array seeding. The given range should contain
   1.553 +    /// any number type and the numbers will be converted to the
   1.554 +    /// architecture word type.
   1.555 +    template <typename Iterator>
   1.556 +    Random(Iterator begin, Iterator end) { 
   1.557 +      typedef typename std::iterator_traits<Iterator>::value_type Number;
   1.558 +      _random_bits::Initializer<Number, Word>::init(core, begin, end);
   1.559 +    }
   1.560 +
   1.561 +    /// \brief Copy constructor
   1.562 +    ///
   1.563 +    /// Copy constructor. The generated sequence will be identical to
   1.564 +    /// the other sequence. It can be used to save the current state
   1.565 +    /// of the generator and later use it to generate the same
   1.566 +    /// sequence.
   1.567 +    Random(const Random& other) {
   1.568 +      core.copyState(other.core);
   1.569 +    }
   1.570 +
   1.571 +    /// \brief Assign operator
   1.572 +    ///
   1.573 +    /// Assign operator. The generated sequence will be identical to
   1.574 +    /// the other sequence. It can be used to save the current state
   1.575 +    /// of the generator and later use it to generate the same
   1.576 +    /// sequence.
   1.577 +    Random& operator=(const Random& other) {
   1.578 +      if (&other != this) {
   1.579 +        core.copyState(other.core);
   1.580 +      }
   1.581 +      return *this;
   1.582 +    }
   1.583 +
   1.584 +    /// \brief Returns a random real number from the range [0, 1)
   1.585 +    ///
   1.586 +    /// It returns a random real number from the range [0, 1). The
   1.587 +    /// default Number type is double.
   1.588 +    template <typename Number>
   1.589 +    Number real() {
   1.590 +      return _random_bits::RealConversion<Number, Word>::convert(core);
   1.591 +    }
   1.592 +
   1.593 +    double real() {
   1.594 +      return real<double>();
   1.595 +    }
   1.596 +
   1.597 +    /// \brief Returns a random real number the range [0, b)
   1.598 +    ///
   1.599 +    /// It returns a random real number from the range [0, b).
   1.600 +    template <typename Number>
   1.601 +    Number real(Number b) { 
   1.602 +      return real<Number>() * b; 
   1.603 +    }
   1.604 +
   1.605 +    /// \brief Returns a random real number from the range [a, b)
   1.606 +    ///
   1.607 +    /// It returns a random real number from the range [a, b).
   1.608 +    template <typename Number>
   1.609 +    Number real(Number a, Number b) { 
   1.610 +      return real<Number>() * (b - a) + a; 
   1.611 +    }
   1.612 +
   1.613 +    /// \brief Returns a random real number from the range [0, 1)
   1.614 +    ///
   1.615 +    /// It returns a random double from the range [0, 1).
   1.616 +    double operator()() {
   1.617 +      return real<double>();
   1.618 +    }
   1.619 +
   1.620 +    /// \brief Returns a random real number from the range [0, b)
   1.621 +    ///
   1.622 +    /// It returns a random real number from the range [0, b).
   1.623 +    template <typename Number>
   1.624 +    Number operator()(Number b) { 
   1.625 +      return real<Number>() * b; 
   1.626 +    }
   1.627 +
   1.628 +    /// \brief Returns a random real number from the range [a, b)
   1.629 +    ///
   1.630 +    /// It returns a random real number from the range [a, b).
   1.631 +    template <typename Number>
   1.632 +    Number operator()(Number a, Number b) { 
   1.633 +      return real<Number>() * (b - a) + a; 
   1.634 +    }
   1.635 +
   1.636 +    /// \brief Returns a random integer from a range
   1.637 +    ///
   1.638 +    /// It returns a random integer from the range {0, 1, ..., b - 1}.
   1.639 +    template <typename Number>
   1.640 +    Number integer(Number b) {
   1.641 +      return _random_bits::Mapping<Number, Word>::map(core, b);
   1.642 +    }
   1.643 +
   1.644 +    /// \brief Returns a random integer from a range
   1.645 +    ///
   1.646 +    /// It returns a random integer from the range {a, a + 1, ..., b - 1}.
   1.647 +    template <typename Number>
   1.648 +    Number integer(Number a, Number b) {
   1.649 +      return _random_bits::Mapping<Number, Word>::map(core, b - a) + a;
   1.650 +    }
   1.651 +
   1.652 +    /// \brief Returns a random integer from a range
   1.653 +    ///
   1.654 +    /// It returns a random integer from the range {0, 1, ..., b - 1}.
   1.655 +    template <typename Number>
   1.656 +    Number operator[](Number b) {
   1.657 +      return _random_bits::Mapping<Number, Word>::map(core, b);
   1.658 +    }
   1.659 +
   1.660 +    /// \brief Returns a random non-negative integer
   1.661 +    ///
   1.662 +    /// It returns a random non-negative integer uniformly from the
   1.663 +    /// whole range of the current \c Number type.  The default result
   1.664 +    /// type of this function is unsigned int.
   1.665 +    template <typename Number>
   1.666 +    Number uinteger() {
   1.667 +      return _random_bits::IntConversion<Number, Word>::convert(core);
   1.668 +    }
   1.669 +
   1.670 +    unsigned int uinteger() {
   1.671 +      return uinteger<unsigned int>();
   1.672 +    }
   1.673 +
   1.674 +    /// \brief Returns a random integer
   1.675 +    ///
   1.676 +    /// It returns a random integer uniformly from the whole range of
   1.677 +    /// the current \c Number type. The default result type of this
   1.678 +    /// function is int.
   1.679 +    template <typename Number>
   1.680 +    Number integer() {
   1.681 +      static const int nb = std::numeric_limits<Number>::digits + 
   1.682 +        (std::numeric_limits<Number>::is_signed ? 1 : 0);
   1.683 +      return _random_bits::IntConversion<Number, Word, nb>::convert(core);
   1.684 +    }
   1.685 +
   1.686 +    int integer() {
   1.687 +      return integer<int>();
   1.688 +    }
   1.689 +    
   1.690 +    /// \brief Returns a random bool
   1.691 +    ///
   1.692 +    /// It returns a random bool. The generator holds a buffer for
   1.693 +    /// random bits. Every time when it become empty the generator makes
   1.694 +    /// a new random word and fill the buffer up.
   1.695 +    bool boolean() {
   1.696 +      return bool_producer.convert(core);
   1.697 +    }
   1.698 +
   1.699 +    ///\name Nonuniform distributions
   1.700 +    ///
   1.701 +    
   1.702 +    ///@{
   1.703 +    
   1.704 +    /// \brief Returns a random bool
   1.705 +    ///
   1.706 +    /// It returns a random bool with given probability of true result
   1.707 +    bool boolean(double p) {
   1.708 +      return operator()() < p;
   1.709 +    }
   1.710 +
   1.711 +    /// Standard Gauss distribution
   1.712 +
   1.713 +    /// Standard Gauss distribution.
   1.714 +    /// \note The Cartesian form of the Box-Muller
   1.715 +    /// transformation is used to generate a random normal distribution.
   1.716 +    /// \todo Consider using the "ziggurat" method instead.
   1.717 +    double gauss() 
   1.718 +    {
   1.719 +      double V1,V2,S;
   1.720 +      do {
   1.721 +	V1=2*real<double>()-1;
   1.722 +	V2=2*real<double>()-1;
   1.723 +	S=V1*V1+V2*V2;
   1.724 +      } while(S>=1);
   1.725 +      return std::sqrt(-2*std::log(S)/S)*V1;
   1.726 +    }
   1.727 +    /// Gauss distribution with given mean and standard deviation
   1.728 +
   1.729 +    /// \sa gauss()
   1.730 +    ///
   1.731 +    double gauss(double mean,double std_dev)
   1.732 +    {
   1.733 +      return gauss()*std_dev+mean;
   1.734 +    }
   1.735 +
   1.736 +    /// Exponential distribution with given mean
   1.737 +
   1.738 +    /// This function generates an exponential distribution random number
   1.739 +    /// with mean <tt>1/lambda</tt>.
   1.740 +    ///
   1.741 +    double exponential(double lambda=1.0)
   1.742 +    {
   1.743 +      return -std::log(real<double>())/lambda;
   1.744 +    }
   1.745 +
   1.746 +    /// Gamma distribution with given integer shape
   1.747 +
   1.748 +    /// This function generates a gamma distribution random number.
   1.749 +    /// 
   1.750 +    ///\param k shape parameter (<tt>k>0</tt> integer)
   1.751 +    double gamma(int k) 
   1.752 +    {
   1.753 +      double s = 0;
   1.754 +      for(int i=0;i<k;i++) s-=std::log(1.0-real<double>());
   1.755 +      return s;
   1.756 +    }
   1.757 +    
   1.758 +    /// Gamma distribution with given shape and scale parameter
   1.759 +
   1.760 +    /// This function generates a gamma distribution random number.
   1.761 +    /// 
   1.762 +    ///\param k shape parameter (<tt>k>0</tt>)
   1.763 +    ///\param theta scale parameter
   1.764 +    ///
   1.765 +    double gamma(double k,double theta=1.0)
   1.766 +    {
   1.767 +      double xi,nu;
   1.768 +      const double delta = k-std::floor(k);
   1.769 +      const double v0=M_E/(M_E-delta);
   1.770 +      do {
   1.771 +	double V0=1.0-real<double>();
   1.772 +	double V1=1.0-real<double>();
   1.773 +	double V2=1.0-real<double>();
   1.774 +	if(V2<=v0) 
   1.775 +	  {
   1.776 +	    xi=std::pow(V1,1.0/delta);
   1.777 +	    nu=V0*std::pow(xi,delta-1.0);
   1.778 +	  }
   1.779 +	else 
   1.780 +	  {
   1.781 +	    xi=1.0-std::log(V1);
   1.782 +	    nu=V0*std::exp(-xi);
   1.783 +	  }
   1.784 +      } while(nu>std::pow(xi,delta-1.0)*std::exp(-xi));
   1.785 +      return theta*(xi-gamma(int(std::floor(k))));
   1.786 +    }
   1.787 +    
   1.788 +      
   1.789 +    ///@}
   1.790 +    
   1.791 +    ///\name Two dimensional distributions
   1.792 +    ///
   1.793 +
   1.794 +    ///@{
   1.795 +    
   1.796 +    /// Uniform distribution on the full unit circle.
   1.797 +    dim2::Point<double> disc() 
   1.798 +    {
   1.799 +      double V1,V2;
   1.800 +      do {
   1.801 +	V1=2*real<double>()-1;
   1.802 +	V2=2*real<double>()-1;
   1.803 +	
   1.804 +      } while(V1*V1+V2*V2>=1);
   1.805 +      return dim2::Point<double>(V1,V2);
   1.806 +    }
   1.807 +    /// A kind of two dimensional Gauss distribution
   1.808 +
   1.809 +    /// This function provides a turning symmetric two-dimensional distribution.
   1.810 +    /// Both coordinates are of standard normal distribution, but they are not
   1.811 +    /// independent.
   1.812 +    ///
   1.813 +    /// \note The coordinates are the two random variables provided by
   1.814 +    /// the Box-Muller method.
   1.815 +    dim2::Point<double> gauss2()
   1.816 +    {
   1.817 +      double V1,V2,S;
   1.818 +      do {
   1.819 +	V1=2*real<double>()-1;
   1.820 +	V2=2*real<double>()-1;
   1.821 +	S=V1*V1+V2*V2;
   1.822 +      } while(S>=1);
   1.823 +      double W=std::sqrt(-2*std::log(S)/S);
   1.824 +      return dim2::Point<double>(W*V1,W*V2);
   1.825 +    }
   1.826 +    /// A kind of two dimensional exponential distribution
   1.827 +
   1.828 +    /// This function provides a turning symmetric two-dimensional distribution.
   1.829 +    /// The x-coordinate is of conditionally exponential distribution
   1.830 +    /// with the condition that x is positive and y=0. If x is negative and 
   1.831 +    /// y=0 then, -x is of exponential distribution. The same is true for the
   1.832 +    /// y-coordinate.
   1.833 +    dim2::Point<double> exponential2() 
   1.834 +    {
   1.835 +      double V1,V2,S;
   1.836 +      do {
   1.837 +	V1=2*real<double>()-1;
   1.838 +	V2=2*real<double>()-1;
   1.839 +	S=V1*V1+V2*V2;
   1.840 +      } while(S>=1);
   1.841 +      double W=-std::log(S)/S;
   1.842 +      return dim2::Point<double>(W*V1,W*V2);
   1.843 +    }
   1.844 +
   1.845 +    ///@}    
   1.846 +  };
   1.847 +
   1.848 +
   1.849 +  extern Random rnd;
   1.850 +
   1.851 +}
   1.852 +
   1.853 +#endif