Add random::poisson distribution
This commit is contained in:
parent
073ac16223
commit
20f3bef6bd
1 changed files with 39 additions and 0 deletions
39
libs/random/include/psemek/random/poisson.hpp
Normal file
39
libs/random/include/psemek/random/poisson.hpp
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <psemek/random/uniform.hpp>
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
namespace psemek::random
|
||||||
|
{
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct poisson_distribution
|
||||||
|
{
|
||||||
|
poisson_distribution(T lambda)
|
||||||
|
: exp_mlambda_(std::exp(-lambda))
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename RNG>
|
||||||
|
std::size_t operator()(RNG & rng)
|
||||||
|
{
|
||||||
|
// Knuth's method
|
||||||
|
|
||||||
|
std::size_t result = 0;
|
||||||
|
T product = T{1};
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
++result;
|
||||||
|
product *= uniform<T>(rng);
|
||||||
|
}
|
||||||
|
while (product > exp_mlambda_);
|
||||||
|
|
||||||
|
return result - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
T exp_mlambda_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue