[llvm-bugs] [Bug 27839] New: Linear congruential generator generates invalid values

via llvm-bugs llvm-bugs at lists.llvm.org
Sun May 22 23:52:31 PDT 2016


https://llvm.org/bugs/show_bug.cgi?id=27839

            Bug ID: 27839
           Summary: Linear congruential generator generates invalid values
           Product: libc++
           Version: 3.7
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: david.g.hammen at gmail.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com
    Classification: Unclassified

The linear congruential generator generates numbers outside the range of
`min()` and `max()` for some values of `a`, `c`, and `m`. For example,
`std::linear_congruential_engine <unsigned long long, 25214903917ull, 11ull,
1ull<<48 >` fails badly. These are the values used by `drand48`.


    #include <random>
    #include <iostream>
    #include <cstdint>

    using drand48_engine = std::linear_congruential_engine <
        std::uint64_t, 25214903917ull, 11ull, (1ull<<48) >;

    int main ()
    {
        drand48_engine rng;
        int pass_fail[2] = {0,0};

        for (int ii = 0; ii < 1000000; ++ii)
        {
            auto num = rng();
            ++pass_fail[num < (1ull<<48)];
        }
        std::cout << "#pass=" << pass_fail[1] << " #fail=" << pass_fail[0] <<
'\n';
    }

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160523/80c0c8da/attachment.html>


More information about the llvm-bugs mailing list