Boost C++ Libraries: Ticket #7020: uniform_01 improvement https://svn.boost.org/trac10/ticket/7020 <p> The current code to simulate uniform random number in interval [0;1] contains verification to skip 1. </p> <blockquote> <p> if (result &lt; result_type(1)) </p> <blockquote> <p> return result; </p> </blockquote> </blockquote> <p> It means that random generator can generate 0 but not 1. This is used in some other code, for example, generate Exponential random number: </p> <blockquote> <p> return -result_type(1) / </p> <blockquote> <p> _lambda * log(result_type(1)-uniform_01&lt;<a class="missing wiki">RealType</a>&gt;()(eng)); </p> </blockquote> </blockquote> <p> Meaning (1 - random number) will never be 0. </p> <p> If eng() returns integer number, then, with some limitations for result_type and generated range of random numbers (result_type has to be double for generated 32 bit random numbers): </p> <blockquote> <p> result_type factor = result_type(1) / (result_type((eng.max)()-(eng.min)()) + result_type(1)); </p> </blockquote> <blockquote> <p> return (result_type(eng() - (eng.min)()) + result_type(0.5)) * factor; </p> </blockquote> <p> In this case, the result will never contain 0 or 1 and will be unbiased. No more checking for 0 or 1 is necessary. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7020 Trac 1.4.3 Steven Watanabe Tue, 16 Oct 2012 17:18:52 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/7020#comment:1 https://svn.boost.org/trac10/ticket/7020#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">invalid</span> </li> </ul> <p> The check for 1 is *already* unnecessary for 32-bit integers and double. It is needed when the mantissa has fewer bits than the integer, because values close to 1 can be rounded to 1. </p> Ticket anonymous Wed, 17 Oct 2012 23:37:51 GMT <link>https://svn.boost.org/trac10/ticket/7020#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7020#comment:2</guid> <description> <p> I see, however it would be better to simulate a random number not including 0 (and 1). For example, this simplify code to generate random numbers from exponential distribution. </p> </description> <category>Ticket</category> </item> </channel> </rss>