Add random::poisson distribution

This commit is contained in:
Nikita Lisitsa 2025-01-12 20:05:35 +03:00
parent 073ac16223
commit 20f3bef6bd

View 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_;
};
}