[Lemon-commits] deba: r3187 - hugo/trunk/lemon

Lemon SVN svn at lemon.cs.elte.hu
Mon Feb 19 20:55:14 CET 2007


Author: deba
Date: Mon Feb 19 20:55:13 2007
New Revision: 3187

Modified:
   hugo/trunk/lemon/random.h

Log:
Adding original license to the file
+ buffered bit generation



Modified: hugo/trunk/lemon/random.h
==============================================================================
--- hugo/trunk/lemon/random.h	(original)
+++ hugo/trunk/lemon/random.h	Mon Feb 19 20:55:13 2007
@@ -16,6 +16,49 @@
  *
  */
 
+/*
+ * This file contains the reimplemented version of the Mersenne Twister
+ * Generator of Matsumoto and Nishimura.
+ *
+ * See the appropriate copyright notice below.
+ * 
+ * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
+ * All rights reserved.                          
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. The names of its contributors may not be used to endorse or promote 
+ *    products derived from this software without specific prior written 
+ *    permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ *
+ * Any feedback is very welcome.
+ * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
+ * email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
+ */
+
 #ifndef LEMON_RANDOM_H
 #define LEMON_RANDOM_H
 
@@ -400,6 +443,25 @@
       }
     };
 
+    template <typename Word>
+    struct BoolProducer {
+      Word buffer;
+      int num;
+      
+      BoolProducer() : num(0) {}
+
+      bool convert(RandomCore<Word>& rnd) {
+        if (num == 0) {
+          buffer = rnd();
+          num = RandomTraits<Word>::bits;
+        }
+        bool r = (buffer & 1);
+        buffer >>= 1;
+        --num;
+        return r;
+      }
+    };
+
   }
 
   /// \ingroup misc
@@ -460,6 +522,8 @@
     typedef unsigned long Word;
     
     _random_bits::RandomCore<Word> core;
+    _random_bits::BoolProducer<Word> bool_producer;
+    
 
   public:
 
@@ -619,9 +683,11 @@
     
     /// \brief Returns a random bool
     ///
-    /// It returns a random bool
+    /// It returns a random bool. The generator holds a buffer for
+    /// random bits. Every time when it become empty the generator makes
+    /// a new random word and fill the buffer up.
     bool boolean() {
-      return _random_bits::BoolConversion<Word>::convert(core);
+      return bool_producer.convert(core);
     }
 
     ///\name Nonuniform distributions



More information about the Lemon-commits mailing list