MLPACK
1.0.4
|
00001 00021 #ifndef __MLPACK_CORE_MATH_RANDOM_HPP 00022 #define __MLPACK_CORE_MATH_RANDOM_HPP 00023 00024 #include <stdlib.h> 00025 #include <math.h> 00026 #include <float.h> 00027 00028 #include <boost/random.hpp> 00029 00030 namespace mlpack { 00031 namespace math { 00032 00033 // Annoying Boost versioning issues. 00034 #include <boost/version.hpp> 00035 00036 #if BOOST_VERSION >= 104700 00037 // Global random object. 00038 extern boost::random::mt19937 randGen; 00039 // Global uniform distribution. 00040 extern boost::random::uniform_01<> randUniformDist; 00041 // Global normal distribution. 00042 extern boost::random::normal_distribution<> randNormalDist; 00043 #else 00044 // Global random object. 00045 extern boost::mt19937 randGen; 00046 00047 #if BOOST_VERSION >= 103900 00048 // Global uniform distribution. 00049 extern boost::uniform_01<> randUniformDist; 00050 #else 00051 // Pre-1.39 Boost.Random did not give default template parameter values. 00052 extern boost::uniform_01<boost::mt19937, double> randUniformDist; 00053 #endif 00054 00055 // Global normal distribution. 00056 extern boost::normal_distribution<> randNormalDist; 00057 #endif 00058 00066 inline void RandomSeed(const size_t seed) 00067 { 00068 randGen.seed((uint32_t) seed); 00069 srand((unsigned int) seed); 00070 } 00071 00075 inline double Random() 00076 { 00077 #if BOOST_VERSION >= 103900 00078 return randUniformDist(randGen); 00079 #else 00080 // Before Boost 1.39, we did not give the random object when we wanted a 00081 // random number; that gets given at construction time. 00082 return randUniformDist(); 00083 #endif 00084 } 00085 00089 inline double Random(const double lo, const double hi) 00090 { 00091 #if BOOST_VERSION >= 103900 00092 return lo + (hi - lo) * randUniformDist(randGen); 00093 #else 00094 // Before Boost 1.39, we did not give the random object when we wanted a 00095 // random number; that gets given at construction time. 00096 return lo + (hi - lo) * randUniformDist(); 00097 #endif 00098 } 00099 00103 inline int RandInt(const int hiExclusive) 00104 { 00105 #if BOOST_VERSION >= 103900 00106 return (int) std::floor((double) hiExclusive * randUniformDist(randGen)); 00107 #else 00108 // Before Boost 1.39, we did not give the random object when we wanted a 00109 // random number; that gets given at construction time. 00110 return (int) std::floor((double) hiExclusive * randUniformDist()); 00111 #endif 00112 } 00113 00117 inline int RandInt(const int lo, const int hiExclusive) 00118 { 00119 #if BOOST_VERSION >= 103900 00120 return lo + (int) std::floor((double)(hiExclusive - lo) 00121 * randUniformDist(randGen)); 00122 #else 00123 // Before Boost 1.39, we did not give the random object when we wanted a 00124 // random number; that gets given at construction time. 00125 return lo + (int) std::floor((double)(hiExclusive - lo) 00126 * randUniformDist()); 00127 #endif 00128 00129 } 00130 00134 inline double RandNormal() 00135 { 00136 return randNormalDist(randGen); 00137 } 00138 00146 inline double RandNormal(const double mean, const double variance) 00147 { 00148 return variance * randNormalDist(randGen) + mean; 00149 } 00150 00151 }; // namespace math 00152 }; // namespace mlpack 00153 00154 #endif // __MLPACK_CORE_MATH_MATH_LIB_HPP