Boost C++ Libraries: Ticket #6059: uniform_real_distribution fails when _min = _max https://svn.boost.org/trac10/ticket/6059 <p> Connected to BUG report <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/6053" title="#6053: Bugs: uniform_real goes into an infinite loop when lb=ub (closed: fixed)">#6053</a>. </p> <p> Even though all asserts require only that _min &lt;= _max, the condition _min=_max seems to be not handled correctly. Consider the code </p> <pre class="wiki"> #include&lt;boost/random/uniform_real_distribution.hpp&gt; #include&lt;boost/random/lagged_fibonacci.hpp&gt; #include&lt;iostream&gt; int main() { boost::lagged_fibonacci607 rng(32); double r = boost::random::uniform_real_distribution&lt;double&gt;(90,91)(rng); std::cout &lt;&lt; r &lt;&lt; std::endl; r = boost::random::uniform_real_distribution&lt;double&gt;(50,50)(rng); std::cout &lt;&lt; r &lt;&lt; std::endl; return 0; } </pre><p> the second std::cout will never execute as an infinite loop is encountered before. The problem is in boost/random/uniform_real_distribution.hpp and in particular at generate_uniform_real line: </p> <pre class="wiki">if(result &lt; max_value) return result; </pre><p> If min_value == max_value the condition above is never met as result = min_value = max_value ALWAYS. </p> <p> A solution (the one I am currently using) could be </p> <pre class="wiki">if ( (result &lt; max_value) || (max_value==min_value) ) return result; </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/6059 Trac 1.4.3 Steven Watanabe Tue, 25 Oct 2011 21:08:12 GMT <link>https://svn.boost.org/trac10/ticket/6059#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6059#comment:1</guid> <description> <p> min == max is illegal, because the range [min, max) is empty. Thus there are no legal values for the distribution to produce. </p> </description> <category>Ticket</category> </item> <item> <author>dario.izzo@…</author> <pubDate>Wed, 26 Oct 2011 16:36:36 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6059#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6059#comment:2</guid> <description> <p> OK, then if this is a design choice, it would be nice to have a warning or an exception be thrown rather then an infinite loop produced (also because the behavior has changed since boost 1.46 (and my code was counting on the previous behavior :) </p> <p> NOTE: I can actually see many reasons to allow min==max and return min, but I guess this choice has already been made .... </p> <p> Thanks for taking the time to answer so promptly </p> </description> <category>Ticket</category> </item> <item> <author>Edward Rudd <urkle@…></author> <pubDate>Sat, 01 Sep 2012 16:55:29 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6059#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6059#comment:3</guid> <description> <p> @steve_watanabe if min == max is illegal then shouldn't the ASSERT in the constructor be _min &lt; _max instead of "&lt;=" as it is now? This is rather diceiving as I expected it to throw some error in the condition of _min == _max (which in my case only occurred to due a bug in my code that calculated the range). Had the assert in uniform_real_distribution been correct I would have been able to find the error quicker. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Sat, 16 Feb 2013 21:14:25 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/6059#comment:4 https://svn.boost.org/trac10/ticket/6059#comment:4 <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> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/82937" title="Fix assertion for uniform_real. Fixes #6053. Fixes #6059.">[82937]</a>) Fix assertion for uniform_real. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/6053" title="#6053: Bugs: uniform_real goes into an infinite loop when lb=ub (closed: fixed)">#6053</a>. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/6059" title="#6059: Bugs: uniform_real_distribution fails when _min = _max (closed: fixed)">#6059</a>. </p> Ticket harris.pc@… Thu, 06 Jun 2013 11:05:28 GMT <link>https://svn.boost.org/trac10/ticket/6059#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6059#comment:5</guid> <description> <p> Why has this bug been fixed, but in boost 1.53.0, the assert remains the same as it was before (incorrect!) </p> <p> See uniform_real_distribution.hpp </p> </description> <category>Ticket</category> </item> <item> <author>harris.pc@…</author> <pubDate>Thu, 06 Jun 2013 11:08:02 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6059#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6059#comment:6</guid> <description> <p> Ah, the patch came after 1.53.0, I think. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>pontus</dc:creator> <pubDate>Wed, 13 Nov 2013 17:49:16 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6059#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6059#comment:7</guid> <description> <p> Still not fixed in 1.55.0 </p> </description> <category>Ticket</category> </item> <item> <author>digigram@…</author> <pubDate>Wed, 04 Dec 2013 19:22:44 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6059#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6059#comment:8</guid> <description> <p> this is a regression from 1.47 where min=max was allowed and returned the expected value. The regression breaks existing software. The logical return would simply be the max or min value when there is no range and so the proposed &gt;= should be right. </p> <p> More fundamental, if the numerator&lt;=divisor and both are greater than 0 than result should always be between min and max (including max) and the for loop really does not make any sense unless max is not allowed to be part of the range, but I would not know why it shouldn't be. In matter of fact, I would say max should be part of the range simply because the max of a dice is 6 and the min is 1 and six can be thrown. </p> <p> So I propose to remove the for loop altogether, and the if assertion as well. </p> </description> <category>Ticket</category> </item> <item> <author>dario.izzo@…</author> <pubDate>Fri, 06 Dec 2013 10:19:52 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6059#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6059#comment:9</guid> <description> <p> I would also love to see this fixed allowing max==min (in which case min is returned). I had to patch all my code manually to allow this behaviour explicitly .... any hope to see this implemented? </p> </description> <category>Ticket</category> </item> </channel> </rss>