1#ifndef VIENNASHE_MATH_RANDOM_HPP
2#define VIENNASHE_MATH_RANDOM_HPP
38 template<
typename ValueT =
double>
52 template<
typename ValueT =
double>
60 void seed(
unsigned int s) { std::srand(s); }
66 ValueT
operator()() {
return static_cast<ValueT
>(std::rand()*(1.0/RAND_MAX)); }
75 template<
typename ValueT =
double>
91 void seed(
unsigned int s) { seed_ = s; }
98 const uint32_t HI_ = 0x80000000;
99 const uint32_t LO_ = 0x7fffffff;
100 const uint32_t A_[2] = { 0, 0x9908b0df };
105 if (index_ == N_+1) this->init();
113 for (i = 0; i < N_-M_; ++i)
115 h = (y_[i] & HI_) | (y_[i+1] & LO_);
116 y_[i] = y_[i+M_] ^ (h >> 1) ^ A_[h & 1];
118 for ( ; i < N_-1; ++i)
120 h = (y_[i] & HI_) | (y_[i+1] & LO_);
121 y_[i] = y_[i+(M_-N_)] ^ (h >> 1) ^ A_[h & 1];
124 h = (y_[N_-1] & HI_) | (y_[0] & LO_);
125 y_[N_-1] = y_[M_-1] ^ (h >> 1) ^ A_[h & 1];
128 value = y_[index_++];
131 value ^= (value >> 11);
132 value ^= (value << 7) & 0x9d2c5680;
133 value ^= (value << 15) & 0xefc60000;
134 value ^= (value >> 18);
136 return (
static_cast<ValueT
>(value)) * (1.0/4294967296.0) ;
145 for (uint32_t i = 1; i < N_; ++i)
146 y_[i] = (1812433253UL * (y_[i-1] ^ (y_[i-1] >> 30)) + i);
149 static const uint32_t N_ = 624;
150 static const uint32_t M_ = 397;
159 template <
typename ResultT =
int>
171 template<
typename RandGeneratorType >
172 ResultT
operator()(RandGeneratorType & gen, ResultT from, ResultT to)
174 return static_cast<ResultT
>(gen() * (to - from) + from);
181 template <
typename RealT =
double,
typename ResultT =
int>
192 this->exp_mean_ = std::exp(- this->mean_);
195 RealT
mean()
const {
return this->mean_; }
202 template <
typename RandGeneratorType>
205 RealT product = RealT(0);
206 for(ResultT m = 0; ; ++m)
208 product += std::log(gen());
209 if(product <= -this->mean_)
222 template <
typename RealT =
double,
typename ResultT =
double>
233 : mean_(
mean), sigma_(
sigma), storedvalue_(0.0) { }
235 RealT
mean()
const {
return this->mean_; }
236 RealT
sigma()
const {
return this->sigma_; }
243 template<
typename RandGeneratorType>
246 double v1, v2, rsq, fac;
247 if (storedvalue_ == 0.0)
251 v1 = 2 * gen() - 1.0;
252 v2 = 2 * gen() - 1.0;
254 }
while (rsq >= 1.0 || rsq == 0.0);
255 fac = std::sqrt(-2.0 * std::log(rsq) / rsq);
256 storedvalue_ = v1 * fac;
257 return this->mean_ + this->sigma_*v2*fac;
263 return this->mean_ + this->sigma_*fac;
A pseudo-random number generator implementation using the merseinne twister MT 19937 cf....
ValueT operator()()
Returns a pseudo-random number using MT19937. Not const, since the internal state is being changed!
merseinne_twister_generator()
Default CTOR to ensure default constructability.
merseinne_twister_generator(unsigned int seed)
CTOR, which accepts the seed to the generator.
void seed(unsigned int s)
Sets the seed of the pseudo-random number generator.
normal_distribution(RealT const &mean, RealT const &sigma)
ResultT operator()(RandGeneratorType &gen)
Returns a Gaussian distributed random number; Algorithm: Box-Muller.
A poisson (exponential) distribution of random numbers.
ResultT operator()(RandGeneratorType &gen)
Returns a poisson distributed random number.
poisson_distribution(RealT const &mean_value)
The basic interface for pseudo-random number generators.
virtual ValueT operator()()=0
The basic random number generator interface (virtual abstract)
virtual ~rand_generator_base()
A pseudo-random number generator implementation based on std::rand()
ValueT operator()()
Calls std::rand divides by RAND_MAX and casts to ValueT.
void seed(unsigned int s)
Sets the seed of the pseudo-random number generator (std::srand)
Contains forward declarations and definition of small classes that must be defined at an early stage.
A logging facility providing fine-grained control over logging in ViennaSHE.
The main ViennaSHE namespace. All functionality resides inside this namespace.