Boost C++ Libraries: Ticket #8491: boost::random::discrete_distribution ignores the first element https://svn.boost.org/trac10/ticket/8491 <p> It seems that discrete_distribution can set the first item weight to 0, which should not be possible. </p> <p> Categorical distribution like this must use all supplied items - of course you use actual weight 0 as a marker of "can't happen", which should be documented. </p> <p> The attached testcase demonstrates the problem. (As well as shows another one that discrete_distribution can't handle negative weights properly.) </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8491 Trac 1.4.3 astralstorm@… Thu, 25 Apr 2013 07:22:07 GMT attachment set https://svn.boost.org/trac10/ticket/8491 https://svn.boost.org/trac10/ticket/8491 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">randoms.cpp</span> </li> </ul> <p> discrete_distribution testcase </p> Ticket Steven Watanabe Wed, 29 May 2013 19:54:14 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/8491#comment:1 https://svn.boost.org/trac10/ticket/8491#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> Negative weights are not allowed. I have no idea what behavior you expect from this. </p> <p> "... w_k, commonly known as the weights, shall be non-negative, non-NaN, and non-infinity." (C++11 26.5.8.6.1 [rand.dist.samp.discrete]) </p> Ticket anonymous Wed, 29 May 2013 21:38:04 GMT status changed; resolution deleted https://svn.boost.org/trac10/ticket/8491#comment:2 https://svn.boost.org/trac10/ticket/8491#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">invalid</span> </li> </ul> <p> The weights are not negative in this testcase - the probs array is actually printed out. Please run the testcase. It offsets them so that they become non-negative. </p> <p> For negative weights, such a normalization should be done within boost anyway I think, but then the special value would have to be -infty instead of 0 to give probability 0, which is an API change. </p> Ticket Steven Watanabe Tue, 11 Mar 2014 18:30:12 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/8491#comment:3 https://svn.boost.org/trac10/ticket/8491#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> You're using invalid iterators in the printing loop. distr.probabilities() returns by value. </p> <p> After I fix that, I get: </p> <pre class="wiki">Weights: 126 215 260 265 277 340 415 Probabilities: 0.0663857 0.113277 0.136986 0.139621 0.145943 0.179136 0.218651 </pre> Ticket AstralStorm Tue, 11 Mar 2014 18:56:25 GMT <link>https://svn.boost.org/trac10/ticket/8491#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8491#comment:4</guid> <description> <p> Thanks, nice catch. It is a bit counterintuitive in terms of API though, I can think of no real use for the by-value semantics here. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Tue, 11 Mar 2014 19:22:34 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8491#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8491#comment:5</guid> <description> <p> by-value is what the standard specifies. In terms of the Boost implementation, it has to be by-value, because it's computed on the fly. The weights aren't actually stored. </p> </description> <category>Ticket</category> </item> </channel> </rss>