diff --git a/include/boost/random/beta_distribution.hpp b/include/boost/random/beta_distribution.hpp index 145bf97..8bd245d 100644 --- a/include/boost/random/beta_distribution.hpp +++ b/include/boost/random/beta_distribution.hpp @@ -101,8 +101,15 @@ public: template RealType operator()(URNG& urng) const { - RealType a = gamma_distribution(_alpha, RealType(1.0))(urng); - RealType b = gamma_distribution(_beta, RealType(1.0))(urng); + RealType a = 0; + RealType b = 0; + + do + { + a = gamma_distribution(_alpha, RealType(1.0))(urng); + b = gamma_distribution(_beta, RealType(1.0))(urng); + } while (a + b == RealType(0)); + return a / (a + b); } diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index f0cf735..46fa05a 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -86,6 +86,8 @@ run test_comp_xoshiro128mm.cpp ; run test_xoshiro128f.cpp /boost/test//boost_unit_test_framework ; run test_comp_xoshiro128f.cpp ; +run github_issue_133.cpp ; + run niederreiter_base2_validate.cpp /boost/test//boost_unit_test_framework ; run sobol_validate.cpp /boost/test//boost_unit_test_framework ; run faure_validate.cpp /boost/test//boost_unit_test_framework ; diff --git a/test/github_issue_133.cpp b/test/github_issue_133.cpp new file mode 100644 index 0000000..561cecc --- /dev/null +++ b/test/github_issue_133.cpp @@ -0,0 +1,25 @@ +// Copyright 2025 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt +// +// See: https://github.com/boostorg/random/issues/133 + +#include +#include +#include +#include + +int main() +{ + constexpr double beta_param = 0.0020368700639848774; + boost::random::beta_distribution dist(beta_param, beta_param); + std::mt19937_64 gen(12345); + + for (int i = 0; i < 10000; ++i) + { + const double Z = dist(gen); + BOOST_TEST(!std::isnan(Z)); + } + + return boost::report_errors(); +}