<random>: Some random number distributions could return NaN · Issue #1174 · microsoft/STL · GitHub
Skip to content

<random>: Some random number distributions could return NaN #1174

Description

@statementreply

Describe the bug

normal_distribution, lognormal_distribution, fisher_f_distribution, and student_t_distribution could return NaN.

Command-line test case

#includes tests/std/tests/GH_001017_discrete_distribution_out_of_range/bad_random_engine.hpp

C:\Users\He\source\repos\STL\out>type random.cpp
#include <cassert>
#include <cmath>
#include <random>

#include "bad_random_engine.hpp"

using namespace std;

template <class Distribution>
void test(Distribution&& distribution) {
    for (bad_random_generator rng; !rng.has_cycled_through();) {
        const auto x = distribution(rng);
        assert(!isnan(x));
    }
}

int main() {
    test(normal_distribution<>{});
    test(lognormal_distribution<>{});
    test(fisher_f_distribution<>{});
    test(student_t_distribution<>{});
}
C:\Users\He\source\repos\STL\out>cl /EHsc /W4 /WX /O2 random.cpp
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.28.29115 版
版权所有(C) Microsoft Corporation。保留所有权利。

random.cpp
Microsoft (R) Incremental Linker Version 14.28.29115.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:random.exe
random.obj

C:\Users\He\source\repos\STL\out>.\random.exe
Assertion failed: !isnan(x), file random.cpp, line 13

STL version

https://github.com/microsoft/STL/commit/e000d3feaf6acaf6214aedcfa21de483931ccbb7

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions