31 #include "boost/format.hpp" 33 #include "gsl/gsl_errno.h" 34 #include "gsl/gsl_randist.h" 36 #include "lsst/pex/exceptions.h" 40 using lsst::pex::policy::Policy;
42 namespace ex = lsst::pex::exceptions;
51 ::gsl_rng_mt19937, ::gsl_rng_ranlxs0, ::gsl_rng_ranlxs1, ::gsl_rng_ranlxs2, ::gsl_rng_ranlxd1,
52 ::gsl_rng_ranlxd2, ::gsl_rng_ranlux, ::gsl_rng_ranlux389, ::gsl_rng_cmrg, ::gsl_rng_mrg,
53 ::gsl_rng_taus, ::gsl_rng_taus2, ::gsl_rng_gfsr4};
56 "MT19937",
"RANLXS0",
"RANLXS1",
"RANLXS2",
"RANLXD1",
"RANLXD2",
"RANLUX",
57 "RANLUX389",
"CMRG",
"MRG",
"TAUS",
"TAUS2",
"GFSR4"};
59 char const *
const Random::_algorithmEnvVarName =
"LSST_RNG_ALGORITHM";
60 char const *
const Random::_seedEnvVarName =
"LSST_RNG_SEED";
64 void Random::initialize() {
66 throw LSST_EXCEPT(ex::InvalidParameterError, (boost::format(
"Invalid RNG seed: %lu") % _seed).str());
68 ::gsl_rng *rng = ::gsl_rng_alloc(_gslRngTypes[_algorithm]);
70 throw LSST_EXCEPT(ex::MemoryError,
"gsl_rng_alloc() failed");
72 ::gsl_rng_set(rng, _seed);
73 _rng.reset(rng, ::gsl_rng_free);
76 void Random::initialize(std::string
const &algorithm) {
79 if (_algorithmNames[i] == algorithm) {
85 throw LSST_EXCEPT(ex::InvalidParameterError,
"RNG algorithm " + algorithm +
" is not supported");
91 if (_algorithm < 0 || _algorithm >= NUM_ALGORITHMS) {
92 throw LSST_EXCEPT(ex::InvalidParameterError,
"Invalid RNG algorithm");
97 Random::Random(std::string
const &algorithm,
unsigned long seed) : _rng(), _seed(seed) {
98 initialize(algorithm);
101 Random::Random(std::shared_ptr<pex::policy::Policy>
const policy) : _rng(), _seed() {
102 std::string
const seed(policy->getString(
"rngSeed"));
104 _seed = std::stoul(seed);
105 }
catch (std::invalid_argument &) {
108 (boost::format(
"Invalid argument in \"rngSeed\" policy value: \"%1%\"") % seed).str());
109 }
catch (std::out_of_range &) {
110 throw LSST_EXCEPT(ex::RuntimeError,
111 (boost::format(
"Out of range in \"rngSeed\" policy value: \"%1%\"") % seed).str());
113 initialize(policy->getString(
"rngAlgorithm"));
118 rng._rng.reset(::gsl_rng_clone(_rng.get()), ::gsl_rng_free);
120 throw LSST_EXCEPT(ex::MemoryError,
"gsl_rng_clone() failed");
132 pex::exceptions::LengthError,
133 (boost::format(
"Size of given state vector (%d) does not match expected size (%d)") %
137 std::copy(state.begin(), state.end(),
static_cast<char *
>(::gsl_rng_state(_rng.get())));
149 static std::vector<std::string> names;
150 if (names.size() == 0) {
152 names.push_back(_algorithmNames[i]);
167 if (n > ::gsl_rng_max(_rng.get()) - ::gsl_rng_min(_rng.get())) {
168 throw LSST_EXCEPT(ex::RangeError,
"Desired random number range exceeds generator range");
170 return ::gsl_rng_uniform_int(_rng.get(), n);
175 double Random::flat(
double const a,
double const b) { return ::gsl_ran_flat(_rng.get(), a, b); }
double poisson(double const mu)
Returns a random variate from the poisson distribution with mean mu.
std::string State
Accessors for the opaque state of the random number generator.
State getState() const
Accessors for the opaque state of the random number generator.
Random(Algorithm algorithm=MT19937, unsigned long seed=1)
Creates a random number generator that uses the given algorithm to produce random numbers...
static std::vector< std::string > const & getAlgorithmNames()
std::string getAlgorithmName() const
unsigned long uniformInt(unsigned long n)
Returns a uniformly distributed random integer from 0 to n-1.
Number of supported algorithms.
void setState(State const &state)
Accessors for the opaque state of the random number generator.
A base class for image defects.
double chisq(double const nu)
Returns a random variate from the chi-squared distribution with nu degrees of freedom.
double flat(double const a, double const b)
Returns a random variate from the flat (uniform) distribution on [a, b).
unsigned long getSeed() const
double gaussian()
Returns a gaussian random variate with mean 0 and standard deviation 1
double uniformPos()
Returns a uniformly distributed random double precision floating point number from the generator...
Random deepCopy() const
Creates a deep copy of this random number generator.
Algorithm
Identifiers for the list of supported algorithms.
std::size_t getStateSize() const
Accessors for the opaque state of the random number generator.
double uniform()
Returns a uniformly distributed random double precision floating point number from the generator...
Algorithm getAlgorithm() const
A class that can be used to generate sequences of random numbers according to a number of different a...