/usr/include/mlpack/core/math/random.hpp is in libmlpack-dev 1.0.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | /**
* @file random.hpp
*
* Miscellaneous math random-related routines.
*
* This file is part of MLPACK 1.0.10.
*
* MLPACK is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* MLPACK is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details (LICENSE.txt).
*
* You should have received a copy of the GNU General Public License along with
* MLPACK. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __MLPACK_CORE_MATH_RANDOM_HPP
#define __MLPACK_CORE_MATH_RANDOM_HPP
#include <mlpack/prereqs.hpp>
#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;
// Global normal distribution.
extern boost::random::normal_distribution<> randNormalDist;
#else
// Global random object.
extern boost::mt19937 randGen;
#if BOOST_VERSION >= 103900
// Global uniform distribution.
extern boost::uniform_01<> randUniformDist;
#else
// Pre-1.39 Boost.Random did not give default template parameter values.
extern boost::uniform_01<boost::mt19937, double> randUniformDist;
#endif
// Global normal distribution.
extern boost::normal_distribution<> randNormalDist;
#endif
/**
* Set the random seed used by the random functions (Random() and RandInt()).
* The seed is casted to a 32-bit integer before being given to the random
* number generator, but a size_t is taken as a parameter for API consistency.
*
* @param seed Seed for the random number generator.
*/
inline void RandomSeed(const size_t seed)
{
randGen.seed((uint32_t) seed);
srand((unsigned int) seed);
}
/**
* Generates a uniform random number between 0 and 1.
*/
inline double Random()
{
#if BOOST_VERSION >= 103900
return randUniformDist(randGen);
#else
// Before Boost 1.39, we did not give the random object when we wanted a
// random number; that gets given at construction time.
return randUniformDist();
#endif
}
/**
* Generates a uniform random number in the specified range.
*/
inline double Random(const double lo, const double hi)
{
#if BOOST_VERSION >= 103900
return lo + (hi - lo) * randUniformDist(randGen);
#else
// Before Boost 1.39, we did not give the random object when we wanted a
// random number; that gets given at construction time.
return lo + (hi - lo) * randUniformDist();
#endif
}
/**
* Generates a uniform random integer.
*/
inline int RandInt(const int hiExclusive)
{
#if BOOST_VERSION >= 103900
return (int) std::floor((double) hiExclusive * randUniformDist(randGen));
#else
// Before Boost 1.39, we did not give the random object when we wanted a
// random number; that gets given at construction time.
return (int) std::floor((double) hiExclusive * randUniformDist());
#endif
}
/**
* Generates a uniform random integer.
*/
inline int RandInt(const int lo, const int hiExclusive)
{
#if BOOST_VERSION >= 103900
return lo + (int) std::floor((double) (hiExclusive - lo)
* randUniformDist(randGen));
#else
// Before Boost 1.39, we did not give the random object when we wanted a
// random number; that gets given at construction time.
return lo + (int) std::floor((double) (hiExclusive - lo)
* randUniformDist());
#endif
}
/**
* 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
#endif // __MLPACK_CORE_MATH_MATH_LIB_HPP
|