This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Indoor/math/distribution/Normal.h
FrankE 62d8d6b36b refactored random subsystem
added compile-time seed support
2016-04-26 15:15:28 +02:00

59 lines
1.1 KiB
C++

#ifndef NORMAL_H
#define NORMAL_H
#include <cmath>
#include <random>
#include "../Random.h"
namespace Distribution {
/** normal distribution */
template <typename T> class Normal {
private:
const T mu;
const T sigma;
const T _a;
RandomGenerator gen;
std::normal_distribution<T> dist;
public:
/** ctor */
Normal(const T mu, const T sigma) :
mu(mu), sigma(sigma), _a(1.0 / (sigma * std::sqrt(2.0 * M_PI))), gen(RANDOM_SEED), dist(mu,sigma) {
}
/** get probability for the given value */
T getProbability(const T val) const {
const T b = -0.5 * ((val-mu)/sigma) * ((val-mu)/sigma);
return _a * std::exp(b);
}
/** get a normally distributed random number */
T draw() {
return dist(gen);
}
/** set the seed to use */
void setSeed(const long seed) {
gen.seed(seed);
}
/** get the probability for the given value */
static T getProbability(const T mu, const T sigma, const T val) {
const T a = 1.0 / (sigma * std::sqrt(2.0 * M_PI));
const T b = -0.5 * ((val-mu)/sigma) * ((val-mu)/sigma);
return a * std::exp(b);
}
};
}
#endif // NORMAL_H