Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r389 - trunk/Core/Math


Chronological Thread 
  • From: bigler@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r389 - trunk/Core/Math
  • Date: Thu, 16 Jun 2005 14:31:33 -0600 (MDT)

Author: bigler
Date: Thu Jun 16 14:31:33 2005
New Revision: 389

Added:
   trunk/Core/Math/CheapRNG.cc
   trunk/Core/Math/CheapRNG.h
Log:
Initial version of CheapRNG.

Added: trunk/Core/Math/CheapRNG.cc
==============================================================================
--- (empty file)
+++ trunk/Core/Math/CheapRNG.cc Thu Jun 16 14:31:33 2005
@@ -0,0 +1,8 @@
+#include <Core/Math/CheapRNG.h>
+#include <limits>
+
+using namespace Manta;
+using namespace std;
+
+float  CheapRNG::inv_maxf = 1.0f/numeric_limits<u_int32_t>::max();
+double CheapRNG::inv_maxd = 1.0 /numeric_limits<u_int32_t>::max();

Added: trunk/Core/Math/CheapRNG.h
==============================================================================
--- (empty file)
+++ trunk/Core/Math/CheapRNG.h  Thu Jun 16 14:31:33 2005
@@ -0,0 +1,93 @@
+/*
+ * CheapRNG
+ *
+ * Cheap refers to the cost to store and to seed the random number.
+ * The speed is about twice as slow as the Mersenne Twister, however
+ * seeding is several orders of magnitude faster.  This is useful if
+ * you want to reseed for only a relatively few samples and you just
+ * want a reasonably random sequence.
+ *
+ * The period for this RNG is 2^32 with ranges of [0..4294967295] (or
+ * [0..2^32-1]).  Consequently the floating point results are
+ * [0..1].  If you want an exclusive interval [0..1) divide genuint()
+ * by 4294967296.0.
+ *
+ * The algorithms are taken from chapter 7 of Numerical Recipies in C:
+ * The Art of Scientific Computing.
+ *
+ * Author: James Bigler
+ * Date: June 16, 2005
+ *
+ */
+
+// All these values are u_int32_t to make sure we have an unsigned 32
+// bit integer, because the computation relies on the overflow
+// properties of integer multiplication.
+
+namespace Manta {
+
+  class CheapRNG {
+    u_int32_t val;
+    
+    // These are used to play some fancy bit twiddling to "create" a
+    // floating point number from the int.  It kills 9 of the bits that
+    // you could have otherwised used, so the period is only 2^23
+    // (8,388,608).
+    enum {
+      float1 0x3f800000,
+      mantissa_mask 0x7fffff
+    };
+
+    // These are for computation help.  They make the interval
+    // returned [0,1].
+    static float  inv_maxf;
+    static double inv_maxd;
+
+    // [0..1]
+    inline float genfloat() {
+      return genuint()*inv_maxf;
+    }
+
+    // [0..1]
+    // See comment above for the enum.
+    inline float genfloatFast() {
+      float fval;
+      *(u_int32_t*)(&fval) = (genuint() & mantissa_mask) | float1;
+      return fval - 1;
+    }
+
+    // [0..1]
+    inline double gendouble() {
+      return genuint()*inv_maxd;
+    }
+  public:
+    CheapRNG(u_int32_t seed_val = 0) {
+      seed(seed_val);
+    }
+    
+    inline void seed(u_int32_t seed_val) {
+      val = seed_val;
+    }
+
+    // Default to returning a double.  In actuallity this should never
+    // be used.
+    template<class T>
+    inline T genRealRand() { return static_cast<T>(gendouble()); }
+
+    inline u_int32_t genuint() {
+      val = 1664525*val + 1013904223;
+      return val;
+    }
+  }; // end class CheapRNG
+
+  template<>
+  inline float CheapRNG::genRealRand<float>() {
+    return genfloat();
+  }
+
+  template<>
+  inline double CheapRNG::genRealRand<double>() {
+    return gendouble();
+  }
+
+}




  • [MANTA] r389 - trunk/Core/Math, bigler, 06/16/2005

Archive powered by MHonArc 2.6.16.

Top of page