Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<random>: piecewise_linear_distribution is still broken #950

Closed
statementreply opened this issue Jul 1, 2020 · 1 comment · Fixed by #1032
Closed

<random>: piecewise_linear_distribution is still broken #950

statementreply opened this issue Jul 1, 2020 · 1 comment · Fixed by #1032
Labels
bug Something isn't working fixed Something works now, yay!

Comments

@statementreply
Copy link
Contributor

statementreply commented Jul 1, 2020

Describe the bug

piecewise_linear_distribution doesn't produce the correct random number distribution.

Command-line test case

G:\Temp>type devcom_421634.cpp
#include <cstdio>
#include <iterator>
#include <random>

using namespace std;

int main() {
    constexpr double intervals[] = {0, 1, 5};
    constexpr double weights[]   = {0, 1, 0};
    constexpr double x           = 2;
    constexpr double probability = 0.55;
    constexpr int total          = 1'000'000;

    piecewise_linear_distribution<> distribution(begin(intervals), end(intervals), begin(weights));
    mt19937 generator;

    int count = 0;
    for (int i = 0; i < total; ++i) {
        if (distribution(generator) < x) {
            ++count;
        }
    }

    const double frequency = static_cast<double>(count) / total;

    printf("Expected probability = %f\n", probability);
    printf("Actual frequency     = %f (%d / %d)\n", frequency, count, total);
}

G:\Temp>cl /EHsc /W4 /WX devcom_421634.cpp
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.26.28806 版
版权所有(C) Microsoft Corporation。保留所有权利。

devcom_421634.cpp
Microsoft (R) Incremental Linker Version 14.26.28806.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:devcom_421634.exe
devcom_421634.obj

G:\Temp>.\devcom_421634.exe
Expected probability = 0.550000
Actual frequency     = 0.718845 (718845 / 1000000)

STL version

Microsoft Visual Studio Community 2019
版本 16.6.3

Additional context

Also tracked on Developer Community as DevCom-421634 and by Microsoft-internal VSO-115939 / AB#115939.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Something works now, yay!
Projects
None yet
2 participants