[mlpack-svn] r10847 - mlpack/trunk/src/mlpack/core/math

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Dec 16 02:32:20 EST 2011


Author: rcurtin
Date: 2011-12-16 02:32:19 -0500 (Fri, 16 Dec 2011)
New Revision: 10847

Added:
   mlpack/trunk/src/mlpack/core/math/random.cpp
Modified:
   mlpack/trunk/src/mlpack/core/math/CMakeLists.txt
   mlpack/trunk/src/mlpack/core/math/random.hpp
Log:
Use Boost random number generators; account for API changes in 1.47.0.


Modified: mlpack/trunk/src/mlpack/core/math/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/core/math/CMakeLists.txt	2011-12-16 06:57:49 UTC (rev 10846)
+++ mlpack/trunk/src/mlpack/core/math/CMakeLists.txt	2011-12-16 07:32:19 UTC (rev 10847)
@@ -6,6 +6,7 @@
   clamp.hpp
   lin_alg.hpp
   random.hpp
+  random.cpp
   range.hpp
   range_impl.hpp
 )

Added: mlpack/trunk/src/mlpack/core/math/random.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/math/random.cpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/core/math/random.cpp	2011-12-16 07:32:19 UTC (rev 10847)
@@ -0,0 +1,25 @@
+/**
+ * @file random.cpp
+ *
+ * Declarations of global Boost random number generators.
+ */
+#include <boost/random.hpp>
+#include <boost/version.hpp>
+
+namespace mlpack {
+namespace math {
+
+#if BOOST_VERSION >= 104700
+  // Global random object.
+  boost::random::mt19937 randGen;
+  // Global uniform distribution.
+  boost::random::uniform_01 randUniformDist;
+#else
+  // Global random object.
+  boost::mt19937 randGen;
+  // Global uniform distribution.
+  boost::uniform_01<> randUniformDist;
+#endif
+
+}; // namespace math
+}; // namespace mlpack

Modified: mlpack/trunk/src/mlpack/core/math/random.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/math/random.hpp	2011-12-16 06:57:49 UTC (rev 10846)
+++ mlpack/trunk/src/mlpack/core/math/random.hpp	2011-12-16 07:32:19 UTC (rev 10847)
@@ -10,15 +10,32 @@
 #include <math.h>
 #include <float.h>
 
+#include <boost/random.hpp>
+
 namespace mlpack {
 namespace math /** Miscellaneous math routines. */ {
 
+// Annoying Boost versioning issues.
+#include <boost/version.hpp>
+
+#if BOOST_VERSION >= 104700
+  // Global random object.
+  extern boost::random::mt19937 randGen;
+  // Global uniform distribution.
+  extern boost::random::uniform_01 randUniformDist;
+#else
+  // Global random object.
+  extern boost::mt19937 randGen;
+  // Global uniform distribution.
+  extern boost::uniform_01<> randUniformDist;
+#endif
+
 /**
  * Generates a uniform random number between 0 and 1.
  */
 inline double Random()
 {
-  return rand() * (1.0 / RAND_MAX);
+  return randUniformDist(randGen);
 }
 
 /**
@@ -26,7 +43,13 @@
  */
 inline double Random(double lo, double hi)
 {
-  return Random() * (hi - lo) + lo;
+  #if BOOST_VERSION >= 104700
+    boost::random::uniform_real_distribution dist(lo, hi);
+  #else
+    boost::uniform_real<> dist(lo, hi);
+  #endif
+
+  return dist(randGen);
 }
 
 /**
@@ -34,7 +57,13 @@
  */
 inline int RandInt(int hi_exclusive)
 {
-  return rand() % hi_exclusive;
+  #if BOOST_VERSION >= 104700
+    boost::random::uniform_int_distribution dist(0, hi_exclusive - 1);
+  #else
+    boost::uniform_int<> dist(0, hi_exclusive - 1);
+  #endif
+
+  return dist(randGen);
 }
 
 /**
@@ -42,7 +71,13 @@
  */
 inline int RandInt(int lo, int hi_exclusive)
 {
-  return (rand() % (hi_exclusive - lo)) + lo;
+  #if BOOST_VERSION >= 104700
+    boost::random::uniform_int_distribution dist(lo, hi_exclusive - 1);
+  #else
+    boost::uniform_int<> dist(0, hi_exclusive - 1);
+  #endif
+
+  return dist(randGen);
 }
 
 }; // namespace math




More information about the mlpack-svn mailing list