Adding original license to the file
authordeba
Mon, 19 Feb 2007 19:55:13 +0000
changeset 23727fcc0179fb21
parent 2371 d2a2cb26ecbb
child 2373 134639e6ea45
Adding original license to the file
+ buffered bit generation
lemon/random.h
     1.1 --- a/lemon/random.h	Mon Feb 19 19:54:28 2007 +0000
     1.2 +++ b/lemon/random.h	Mon Feb 19 19:55:13 2007 +0000
     1.3 @@ -16,6 +16,49 @@
     1.4   *
     1.5   */
     1.6  
     1.7 +/*
     1.8 + * This file contains the reimplemented version of the Mersenne Twister
     1.9 + * Generator of Matsumoto and Nishimura.
    1.10 + *
    1.11 + * See the appropriate copyright notice below.
    1.12 + * 
    1.13 + * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
    1.14 + * All rights reserved.                          
    1.15 + *
    1.16 + * Redistribution and use in source and binary forms, with or without
    1.17 + * modification, are permitted provided that the following conditions
    1.18 + * are met:
    1.19 + *
    1.20 + * 1. Redistributions of source code must retain the above copyright
    1.21 + *    notice, this list of conditions and the following disclaimer.
    1.22 + *
    1.23 + * 2. Redistributions in binary form must reproduce the above copyright
    1.24 + *    notice, this list of conditions and the following disclaimer in the
    1.25 + *    documentation and/or other materials provided with the distribution.
    1.26 + *
    1.27 + * 3. The names of its contributors may not be used to endorse or promote 
    1.28 + *    products derived from this software without specific prior written 
    1.29 + *    permission.
    1.30 + *
    1.31 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.32 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.33 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    1.34 + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
    1.35 + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.36 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.37 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    1.38 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.39 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.40 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.41 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    1.42 + * OF THE POSSIBILITY OF SUCH DAMAGE.
    1.43 + *
    1.44 + *
    1.45 + * Any feedback is very welcome.
    1.46 + * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
    1.47 + * email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
    1.48 + */
    1.49 +
    1.50  #ifndef LEMON_RANDOM_H
    1.51  #define LEMON_RANDOM_H
    1.52  
    1.53 @@ -400,6 +443,25 @@
    1.54        }
    1.55      };
    1.56  
    1.57 +    template <typename Word>
    1.58 +    struct BoolProducer {
    1.59 +      Word buffer;
    1.60 +      int num;
    1.61 +      
    1.62 +      BoolProducer() : num(0) {}
    1.63 +
    1.64 +      bool convert(RandomCore<Word>& rnd) {
    1.65 +        if (num == 0) {
    1.66 +          buffer = rnd();
    1.67 +          num = RandomTraits<Word>::bits;
    1.68 +        }
    1.69 +        bool r = (buffer & 1);
    1.70 +        buffer >>= 1;
    1.71 +        --num;
    1.72 +        return r;
    1.73 +      }
    1.74 +    };
    1.75 +
    1.76    }
    1.77  
    1.78    /// \ingroup misc
    1.79 @@ -460,6 +522,8 @@
    1.80      typedef unsigned long Word;
    1.81      
    1.82      _random_bits::RandomCore<Word> core;
    1.83 +    _random_bits::BoolProducer<Word> bool_producer;
    1.84 +    
    1.85  
    1.86    public:
    1.87  
    1.88 @@ -619,9 +683,11 @@
    1.89      
    1.90      /// \brief Returns a random bool
    1.91      ///
    1.92 -    /// It returns a random bool
    1.93 +    /// It returns a random bool. The generator holds a buffer for
    1.94 +    /// random bits. Every time when it become empty the generator makes
    1.95 +    /// a new random word and fill the buffer up.
    1.96      bool boolean() {
    1.97 -      return _random_bits::BoolConversion<Word>::convert(core);
    1.98 +      return bool_producer.convert(core);
    1.99      }
   1.100  
   1.101      ///\name Nonuniform distributions