Skip to content

<random>: piecewise_linear_distribution is still broken #950

Description

@statementreply

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions