diff --git a/libs/random/include/psemek/random/poisson.hpp b/libs/random/include/psemek/random/poisson.hpp new file mode 100644 index 00000000..d86dfc20 --- /dev/null +++ b/libs/random/include/psemek/random/poisson.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include + +#include + +namespace psemek::random +{ + + template + struct poisson_distribution + { + poisson_distribution(T lambda) + : exp_mlambda_(std::exp(-lambda)) + {} + + template + std::size_t operator()(RNG & rng) + { + // Knuth's method + + std::size_t result = 0; + T product = T{1}; + + do + { + ++result; + product *= uniform(rng); + } + while (product > exp_mlambda_); + + return result - 1; + } + + private: + T exp_mlambda_; + }; + +}