33#include "boost/format.hpp"
35#include "gsl/gsl_randist.h"
50 ::gsl_rng_mt19937, ::gsl_rng_ranlxs0, ::gsl_rng_ranlxs1, ::gsl_rng_ranlxs2, ::gsl_rng_ranlxd1,
51 ::gsl_rng_ranlxd2, ::gsl_rng_ranlux, ::gsl_rng_ranlux389, ::gsl_rng_cmrg, ::gsl_rng_mrg,
52 ::gsl_rng_taus, ::gsl_rng_taus2, ::gsl_rng_gfsr4};
55 "MT19937",
"RANLXS0",
"RANLXS1",
"RANLXS2",
"RANLXD1",
"RANLXD2",
"RANLUX",
56 "RANLUX389",
"CMRG",
"MRG",
"TAUS",
"TAUS2",
"GFSR4"};
58char const *
const Random::_algorithmEnvVarName =
"LSST_RNG_ALGORITHM";
59char const *
const Random::_seedEnvVarName =
"LSST_RNG_SEED";
63void Random::initialize() {
64 ::gsl_rng *rng = ::gsl_rng_alloc(_gslRngTypes[_algorithm]);
71 ::gsl_rng_set(rng, useSeed);
72 _rng.reset(rng, ::gsl_rng_free);
75void Random::initialize(
std::string const &algorithm) {
78 if (_algorithmNames[i] == algorithm) {
97 initialize(algorithm);
102 rng._rng.reset(::gsl_rng_clone(_rng.get()), ::gsl_rng_free);
117 (
boost::format(
"Size of given state vector (%d) does not match expected size (%d)") %
121 std::copy(state.
begin(), state.
end(),
static_cast<char *
>(::gsl_rng_state(_rng.get())));
134 if (names.
size() == 0) {
135 for (
auto _algorithmName : _algorithmNames) {
151 if (n > ::gsl_rng_max(_rng.get()) - ::gsl_rng_min(_rng.get())) {
154 return ::gsl_rng_uniform_int(_rng.get(), n);
159double Random::flat(
double const a,
double const b) { return ::gsl_ran_flat(_rng.get(),
a,
b); }
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
A class that can be used to generate sequences of random numbers according to a number of different a...
Random(Algorithm algorithm=MT19937, unsigned long seed=1)
Creates a random number generator that uses the given algorithm to produce random numbers,...
Algorithm
Identifiers for the list of supported algorithms.
@ NUM_ALGORITHMS
Number of supported algorithms.
double uniformPos()
Returns a uniformly distributed random double precision floating point number from the generator.
double chisq(double const nu)
Returns a random variate from the chi-squared distribution with nu degrees of freedom.
Random deepCopy() const
Creates a deep copy of this random number generator.
static std::vector< std::string > const & getAlgorithmNames()
double poisson(double const mu)
Returns a random variate from the poisson distribution with mean mu.
Algorithm getAlgorithm() const
std::string getAlgorithmName() const
unsigned long getSeed() const
double gaussian()
Returns a gaussian random variate with mean 0 and standard deviation 1
void setState(State const &state)
std::string State
Accessors for the opaque state of the random number generator.
std::size_t getStateSize() const
double uniform()
Returns a uniformly distributed random double precision floating point number from the generator.
unsigned long uniformInt(unsigned long n)
Returns a uniformly distributed random integer from 0 to n-1.
double flat(double const a, double const b)
Returns a random variate from the flat (uniform) distribution on [a, b).
Reports invalid arguments.
Reports attempts to exceed implementation-defined length limits for some classes.
Reports when the result of an operation cannot be represented by the destination type.
T emplace_back(T... args)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
A base class for image defects.