<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - std::piecewise_constant_distribution calculates wrong densities"
   href="http://llvm.org/bugs/show_bug.cgi?id=22805">22805</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>std::piecewise_constant_distribution calculates wrong densities
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>gnuetzi@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu, mclow.lists@gmail.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>It seems that ``std::piecewise_constant_distribution`` computes the densities
wrongly , at least with GCC and its standard library.

According to
<a href="http://www.cplusplus.com/reference/random/piecewise_constant_distribution/">http://www.cplusplus.com/reference/random/piecewise_constant_distribution/</a>:
The densities should be computed as:
![enter image description here][1]

Checking this manually reveals the bug!

This can be seen here: <a href="http://coliru.stacked-crooked.com/a/ca171bf600b5148f">http://coliru.stacked-crooked.com/a/ca171bf600b5148f</a>

The source code related to this is found in
`/usr/include/c++/4.8/bits/random.tcc` (on linux) and the extract of the
initialization function `_M_initialize` called by the constructor shows that
there is something incorrect here:



    const double __sum = std::accumulate(_M_den.begin(),
                           _M_den.end(), 0.0);

          __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
                    __sum);  <----- WRONG

          // THIS is not the cummulative distribution (since the above
normalization does not give the probability of the intervalls!)
          _M_cp.reserve(_M_den.size());
          std::partial_sum(_M_den.begin(), _M_den.end(),
                   std::back_inserter(_M_cp));


          // Make sure the last cumulative probability is one.
          _M_cp[_M_cp.size() - 1] = 1.0;



          // Dividing here by the interval length is WRONG!!!

          for (size_t __k = 0; __k < _M_den.size(); ++__k)
               _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];




  [1]: <a href="http://i.stack.imgur.com/LDthp.gif">http://i.stack.imgur.com/LDthp.gif</a></pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>