34 #include "boost/format.hpp"
36 #include "gsl/gsl_errno.h"
37 #include "gsl/gsl_randist.h"
45 namespace ex = lsst::pex::exceptions;
46 namespace math = lsst::afw::math;
102 throw LSST_EXCEPT(ex::MemoryError,
"gsl_rng_alloc() failed");
104 ::gsl_rng_set(rng,
_seed);
105 _rng.reset(rng, ::gsl_rng_free);
120 for (
int i = 0; i < NUM_ALGORITHMS; ++i) {
121 if (_algorithmNames[i] == algorithm) {
127 throw LSST_EXCEPT(ex::InvalidParameterError,
"RNG algorithm " +
128 algorithm +
" is not supported");
151 : _rng(), _seed(seed), _algorithm(algorithm)
154 throw LSST_EXCEPT(ex::InvalidParameterError,
"Invalid RNG algorithm");
175 : _rng(), _seed(seed)
202 std::string
const seed(policy->getString(
"rngSeed"));
204 _seed = std::stoul(seed);
205 }
catch(std::invalid_argument &) {
207 (
boost::format(
"Invalid argument in \"rngSeed\" policy value: \"%1%\"") % seed).str());
208 }
catch(std::out_of_range &) {
210 (
boost::format(
"Out of range in \"rngSeed\" policy value: \"%1%\"") % seed).str());
212 initialize(policy->getString(
"rngAlgorithm"));
227 rng.
_rng.reset(::gsl_rng_clone(_rng.get()), ::gsl_rng_free);
229 throw LSST_EXCEPT(ex::MemoryError,
"gsl_rng_clone() failed");
235 return State(static_cast<char*>(::gsl_rng_state(_rng.get())), getStateSize());
239 if (state.size() != getStateSize()) {
241 pex::exceptions::LengthError,
242 (
boost::format(
"Size of given state vector (%d) does not match expected size (%d)")
243 % state.size() % getStateSize()).str()
246 std::copy(state.begin(), state.end(),
static_cast<char*
>(::gsl_rng_state(_rng.get())));
250 return ::gsl_rng_size(_rng.get());
266 return std::string(_algorithmNames[_algorithm]);
273 static std::vector<std::string> names;
274 if (names.size() == 0) {
275 for (
int i = 0; i < NUM_ALGORITHMS; ++i) {
276 names.push_back(_algorithmNames[i]);
307 return ::gsl_rng_uniform(_rng.get());
322 return ::gsl_rng_uniform_pos(_rng.get());
344 if (n > ::gsl_rng_max(_rng.get()) - ::gsl_rng_min(_rng.get())) {
346 "Desired random number range exceeds generator range");
348 return ::gsl_rng_uniform_int(_rng.get(), n);
361 return ::gsl_ran_flat(_rng.get(), a,
b);
373 return ::gsl_ran_gaussian_ziggurat(_rng.get(), 1.0);
383 return ::gsl_ran_chisq(_rng.get(), nu);
394 return ::gsl_ran_poisson(_rng.get(), mu);
Random(Algorithm algorithm=MT19937, unsigned long seed=1)
double poisson(double const nu)
a container for holding hierarchical configuration data in memory.
std::shared_ptr< Policy > Ptr
unsigned long uniformInt(unsigned long n)
unsigned long getSeed() const
static char const *const _seedEnvVarName
std::size_t getStateSize() const
static char const *const _algorithmEnvVarName
void setState(State const &state)
Algorithm getAlgorithm() const
double chisq(double const nu)
std::string getAlgorithmName() const
double flat(double const a, double const b)
static char const *const _algorithmNames[NUM_ALGORITHMS]
std::shared_ptr< ::gsl_rng > _rng
#define LSST_EXCEPT(type,...)
Random number generator class.
afw::table::Key< double > b
Include files required for standard LSST Exception handling.
::gsl_rng_type const *const _gslRngTypes[NUM_ALGORITHMS]
static std::vector< std::string > const & getAlgorithmNames()