63 lines
1.3 KiB
C++
63 lines
1.3 KiB
C++
/*
|
||
* © Copyright 2014 – Urheberrechtshinweis
|
||
* Alle Rechte vorbehalten / All Rights Reserved
|
||
*
|
||
* Programmcode ist urheberrechtlich geschuetzt.
|
||
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
|
||
* Keine Verwendung ohne explizite Genehmigung.
|
||
* (vgl. § 106 ff UrhG / § 97 UrhG)
|
||
*/
|
||
|
||
#ifndef EXPONENTIAL_H
|
||
#define EXPONENTIAL_H
|
||
|
||
#include <cmath>
|
||
#include <random>
|
||
#include "../random/RandomGenerator.h"
|
||
|
||
namespace Distribution {
|
||
|
||
/** exponential distribution */
|
||
template <typename T> class Exponential {
|
||
|
||
private:
|
||
|
||
const T lambda;
|
||
|
||
Random::RandomGenerator gen;
|
||
std::exponential_distribution<T> dist;
|
||
|
||
public:
|
||
|
||
/** ctor */
|
||
Exponential(const T lambda) : lambda(lambda), gen(RANDOM_SEED), dist(lambda) {
|
||
;
|
||
}
|
||
|
||
/** get probability for the given value */
|
||
T getProbability(const T val) const {
|
||
return getProbability(lambda, val);
|
||
}
|
||
|
||
/** get an exponentially 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 lambda, const T val) {
|
||
return (val < 0) ? (0) : (lambda * std::exp( -lambda * val ));
|
||
}
|
||
|
||
};
|
||
|
||
}
|
||
|
||
#endif // EXPONENTIAL_H
|