<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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::uniform_int_distribution<__int128_t>{INT64_MIN, INT64_MAX} produces out-of-range values"
   href="https://bugs.llvm.org/show_bug.cgi?id=51520">51520</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>std::uniform_int_distribution<__int128_t>{INT64_MIN, INT64_MAX} produces out-of-range values
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>12.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </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>moritz.klammler@cetitec.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The specific std::uniform_int_distribution<__int128_t>{INT64_MIN, INT64_MAX}
produces values that are not in range. Changing any of the parameters -- e.g.
using std::int64_t as type or making either of the limits smaller or larger --
causes the problem to go away. The incorrect values are completely
out-of-range, not marginal off-by-one errors.

I have created the following test program and could reproduce the issue with
every version of libc++ and set of compiler options I've tried; the problem
never occurs with -stdlib=libstdc++ so it seems to me to be a library rather
than a compiler issue.

<a href="https://godbolt.org/z/z5WYsKEv6">https://godbolt.org/z/z5WYsKEv6</a>

This is a minimal version (with no output) of the demo linked above to
reproduce the problem:

#include <cstdint>
#include <cstdlib>
#include <random>

int main()
{
    auto engine = std::default_random_engine{};
    auto distro = std::uniform_int_distribution<__int128_t>{INT64_MIN,
INT64_MAX};
    for (auto i = 0; i < 1000; ++i) {
        const auto n = distro(engine);
        if ((n < distro.min()) || (n > distro.max())) {
            return EXIT_FAILURE;
        }
    }
    return EXIT_SUCCESS;
}</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>