Skip to content

Linear congruential generator generates invalid values #28213

@llvmbot

Description

@llvmbot
Bugzilla Link 27839
Resolution FIXED
Resolved on May 24, 2021 21:04
Version 3.7
OS All
Reporter LLVM Bugzilla Contributor
CC @hfinkel,@mclow,@utsumi-fj,@zoecarver

Extended Description

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';
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillalibc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions