Possible construction bug in std::poisson_distribution

Viewed 111

I am not sure this is the right platform for this comment/question, however consider the following code snippet:

#include <random>
#include <iostream>
#include <limits>
int main()
{
    int m=std::numeric_limits<int>::max();
    std::cout<<m<<"\n";
    std::poisson_distribution<int> p(m+1.);//implicit conversion to double
    std::mt19937 rng(std::random_device{}());
    auto x=p(rng);
    std::cout<<x<<std::endl;
    return 0;
}

The code compiles and runs. Though clearly it is not sampling correctly the distribution since 50% of the times x should be larger that m+1, something not allowed. On my machine the program spits out only numbers smaller than m but this may be a sort of undefined behavior. In fact if I print std::cout<<p.max()<<"\n"; I obtain m. Now, I know that mathematically speaking I am essentially sampling from a normal distribution and I could easily go around the issue. However I am wondering if a a run-time exception should be thrown when the distribution is constructed or if a note should be added in the following page: https://en.cppreference.com/w/cpp/numeric/random/poisson_distribution. In the case you agree with me who should I contact?

1 Answers

All Poisson distributions have support over all non-negative integers. As such, there are always values that are not generated because they are too large. One could argue either that such values should be clamped to the range or that they be redrawn. For reasonable values of λ, neither has any practical effect, but picking an unusual parameter will force the inevitable choice to be a dominant effect.

Related