Use Boost random number generators; account for API changes in 1.47.0.

This commit is contained in:
Ryan Curtin
2011-12-16 07:32:19 +00:00
parent 5394afd152
commit e9ab4c87f5
3 changed files with 65 additions and 4 deletions
+1
View File
@@ -6,6 +6,7 @@ set(SOURCES
clamp.hpp
lin_alg.hpp
random.hpp
random.cpp
range.hpp
range_impl.hpp
)
+25
View File
@@ -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
+39 -4
View File
@@ -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()
*/
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 double Random(double lo, double hi)
*/
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 hi_exclusive)
*/
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