Boost C++ Libraries: Ticket #9652: Infinite loop in random::binomial_distribution https://svn.boost.org/trac10/ticket/9652 <p> There is a potential infinite loop in binomial_distribution::invert(). The problem is caused by round-off errors. The condition required for the infinite loop is: </p> <p> t (no. trials) is large<br /> p (probability) is small<br /> uniform_01 returns a number very close to 1.<br /> </p> <p> I found this problem in Boost 1.48.0, but the relevant part of the code is the same in development trunk. </p> <p> The issue can be reproduced by the following program. </p> <pre class="wiki">#include &lt;iostream&gt; #include "boost/random/binomial_distribution.hpp" /* PSEUDO uniform random number generater. */ class URNG { public: typedef unsigned long int result_type; result_type operator()() {return _ret_val;} result_type min() const {return 0;} result_type max() const {return std::numeric_limits&lt;result_type&gt;::max();} URNG(result_type ret_val) : _ret_val(ret_val) {} private: result_type _ret_val; }; int main(void) { typedef boost::random::binomial_distribution&lt;long int, double&gt; Binom; Binom binom(41344,4.87026e-05); URNG urng_small(1024); std::cout &lt;&lt; "Draw a number from a binomial distribution." &lt;&lt; std::endl; std::cout &lt;&lt; "binom returns " &lt;&lt; binom(urng_small) &lt;&lt; std::endl; URNG urng_big(std::numeric_limits&lt;unsigned long int&gt;::max() - 1024); std::cout &lt;&lt; "Draw again a number from a binomial distribution." &lt;&lt; std::endl; std::cout &lt;&lt; "binom does not return (infinite loop) " &lt;&lt; binom(urng_big) &lt;&lt; std::endl; return 0; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9652 Trac 1.4.3 Steven Watanabe Tue, 11 Mar 2014 20:04:00 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/9652#comment:1 https://svn.boost.org/trac10/ticket/9652#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Fixed in <a class="ext-link" href="http://github.com/boostorg/random/commit/9cd247da37bd9b13c06956c4d3901376f0e00722"><span class="icon">​</span>http://github.com/boostorg/random/commit/9cd247da37bd9b13c06956c4d3901376f0e00722</a>. </p> Ticket