[mlpack-svn] r11536 - mlpack/trunk/src/mlpack/core/math
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Feb 16 12:47:41 EST 2012
Author: rcurtin
Date: 2012-02-16 12:47:40 -0500 (Thu, 16 Feb 2012)
New Revision: 11536
Modified:
mlpack/trunk/src/mlpack/core/math/random.cpp
mlpack/trunk/src/mlpack/core/math/random.hpp
Log:
Add RandNormal() and fix some const-correctness semi-issues.
Modified: mlpack/trunk/src/mlpack/core/math/random.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/math/random.cpp 2012-02-16 04:41:06 UTC (rev 11535)
+++ mlpack/trunk/src/mlpack/core/math/random.cpp 2012-02-16 17:47:40 UTC (rev 11536)
@@ -14,11 +14,15 @@
boost::random::mt19937 randGen;
// Global uniform distribution.
boost::random::uniform_01<> randUniformDist;
+ // Global normal distribution.
+ boost::random::normal_distribution<> randNormalDist;
#else
// Global random object.
boost::mt19937 randGen;
// Global uniform distribution.
boost::uniform_01<> randUniformDist;
+ // Global normal distribution.
+ boost::normal_distribution<> randNormalDist;
#endif
}; // namespace math
Modified: mlpack/trunk/src/mlpack/core/math/random.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/math/random.hpp 2012-02-16 04:41:06 UTC (rev 11535)
+++ mlpack/trunk/src/mlpack/core/math/random.hpp 2012-02-16 17:47:40 UTC (rev 11536)
@@ -23,11 +23,15 @@
extern boost::random::mt19937 randGen;
// Global uniform distribution.
extern boost::random::uniform_01<> randUniformDist;
+ // Global normal distribution.
+ extern boost::random::normal_distribution<> randNormalDist;
#else
// Global random object.
extern boost::mt19937 randGen;
// Global uniform distribution.
extern boost::uniform_01<> randUniformDist;
+ // Global normal distribution.
+ extern boost::normal_distribution<> randNormalDist;
#endif
/**
@@ -54,7 +58,7 @@
/**
* Generates a uniform random number in the specified range.
*/
-inline double Random(double lo, double hi)
+inline double Random(const double lo, const double hi)
{
#if BOOST_VERSION >= 104700
boost::random::uniform_real_distribution<> dist(lo, hi);
@@ -68,7 +72,7 @@
/**
* Generates a uniform random integer.
*/
-inline int RandInt(int hiExclusive)
+inline int RandInt(const int hiExclusive)
{
#if BOOST_VERSION >= 104700
boost::random::uniform_int_distribution<> dist(0, hiExclusive - 1);
@@ -82,7 +86,7 @@
/**
* Generates a uniform random integer.
*/
-inline int RandInt(int lo, int hiExclusive)
+inline int RandInt(const int lo, const int hiExclusive)
{
#if BOOST_VERSION >= 104700
boost::random::uniform_int_distribution<> dist(lo, hiExclusive - 1);
@@ -93,6 +97,26 @@
return dist(randGen);
}
+/**
+ * Generates a normally distributed random number with mean 0 and variance 1.
+ */
+inline double RandNormal()
+{
+ return randNormalDist(randGen);
+}
+
+/**
+ * Generates a normally distributed random number with specified mean and
+ * variance.
+ *
+ * @param mean Mean of distribution.
+ * @param variance Variance of distribution.
+ */
+inline double RandNormal(const double mean, const double variance)
+{
+ return variance * randNormalDist(randGen) + mean;
+}
+
}; // namespace math
}; // namespace mlpack
More information about the mlpack-svn
mailing list