34 #include "boost/format.hpp"
35 #include "boost/lexical_cast.hpp"
37 #include "gsl/gsl_errno.h"
38 #include "gsl/gsl_randist.h"
46 namespace ex = lsst::pex::exceptions;
47 namespace math = lsst::afw::math;
103 throw LSST_EXCEPT(ex::MemoryError,
"gsl_rng_alloc() failed");
105 ::gsl_rng_set(rng,
_seed);
106 _rng.reset(rng, ::gsl_rng_free);
121 for (
int i = 0; i < NUM_ALGORITHMS; ++i) {
122 if (_algorithmNames[i] == algorithm) {
128 throw LSST_EXCEPT(ex::InvalidParameterError,
"RNG algorithm " +
129 algorithm +
" is not supported");
152 : _rng(), _seed(seed), _algorithm(algorithm)
155 throw LSST_EXCEPT(ex::InvalidParameterError,
"Invalid RNG algorithm");
176 : _rng(), _seed(seed)
203 std::string
const seed(policy->getString(
"rngSeed"));
205 _seed = boost::lexical_cast<
unsigned long>(seed);
206 }
catch(boost::bad_lexical_cast &) {
208 (
boost::format(
"Invalid \"rngSeed\" policy value: \"%1%\"") % seed).str());
210 initialize(policy->getString(
"rngAlgorithm"));
225 rng.
_rng.reset(::gsl_rng_clone(_rng.get()), ::gsl_rng_free);
227 throw LSST_EXCEPT(ex::MemoryError,
"gsl_rng_clone() failed");
233 return State(static_cast<char*>(::gsl_rng_state(_rng.get())), getStateSize());
237 if (state.size() != getStateSize()) {
239 pex::exceptions::LengthError,
240 (
boost::format(
"Size of given state vector (%d) does not match expected size (%d)")
241 % state.size() % getStateSize()).str()
244 std::copy(state.begin(), state.end(),
static_cast<char*
>(::gsl_rng_state(_rng.get())));
248 return ::gsl_rng_size(_rng.get());
264 return std::string(_algorithmNames[_algorithm]);
271 static std::vector<std::string> names;
272 if (names.size() == 0) {
273 for (
int i = 0; i < NUM_ALGORITHMS; ++i) {
274 names.push_back(_algorithmNames[i]);
305 return ::gsl_rng_uniform(_rng.get());
320 return ::gsl_rng_uniform_pos(_rng.get());
342 if (n > ::gsl_rng_max(_rng.get()) - ::gsl_rng_min(_rng.get())) {
344 "Desired random number range exceeds generator range");
346 return ::gsl_rng_uniform_int(_rng.get(), n);
359 return ::gsl_ran_flat(_rng.get(), a,
b);
371 return ::gsl_ran_gaussian_ziggurat(_rng.get(), 1.0);
381 return ::gsl_ran_chisq(_rng.get(), nu);
392 return ::gsl_ran_poisson(_rng.get(), mu);
std::string getAlgorithmName() const
static char const *const _seedEnvVarName
Algorithm getAlgorithm() const
double chisq(double const nu)
unsigned long getSeed() const
double flat(double const a, double const b)
a container for holding hierarchical configuration data in memory.
SelectEigenView< T >::Type copy(Eigen::EigenBase< T > const &other)
Copy an arbitrary Eigen expression into a new EigenView.
std::size_t getStateSize() const
boost::shared_ptr< Policy > Ptr
::gsl_rng_type const *const _gslRngTypes[NUM_ALGORITHMS]
double poisson(double const nu)
Random(Algorithm algorithm=MT19937, unsigned long seed=1)
unsigned long uniformInt(unsigned long n)
static char const *const _algorithmEnvVarName
void setState(State const &state)
#define LSST_EXCEPT(type,...)
Random number generator class.
static std::vector< std::string > const & getAlgorithmNames()
static char const *const _algorithmNames[NUM_ALGORITHMS]
afw::table::Key< double > b
Include files required for standard LSST Exception handling.
boost::shared_ptr< ::gsl_rng > _rng