From 20f3bef6bdbf1b2463f9b8560cfce8e7e5f02eb7 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sun, 12 Jan 2025 20:05:35 +0300 Subject: [PATCH] Add random::poisson distribution --- libs/random/include/psemek/random/poisson.hpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 libs/random/include/psemek/random/poisson.hpp 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_; + }; + +}