Skip to content

libc++: Wrong Initial Distribution When Varying the Seed #114829

@jonas-eschmann

Description

@jonas-eschmann

The default random engine (std::minstd_rand0) produces a highly correlated initial distribution when varying the seeds (tested with clang 19.1.0 and seems to be the same for all prior versions):

#include <iostream>
#include <random>

int main(){
    for(int seed = 0; seed < 100; seed++){
        auto rng = std::default_random_engine(seed);
        float value = std::uniform_real_distribution<float>(0, 1)(rng);
        std::cout << value << "\n";
    }
    return 0;
}

Output

...
0.000101742
0.000109569
0.000117395
0.000125221
0.000133048
0.000140874
0.000148701
0.000156527
0.000164353
0.00017218
0.000180006
0.000187832
0.000195659
0.000203485
0.000211312
0.000219138
0.000226964
0.000234791
0.000242617
0.000250443
0.00025827
0.000266096
0.000273922
0.000281749
0.000289575
0.000297402
0.000305228
0.000313054
0.000320881
0.000328707
0.000336533
0.00034436
...

https://godbolt.org/z/x7jrEcaME

I think users would expect this to be uniformly distributed. GCC has the same issue but MSVC seems fine

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidResolved as invalid, i.e. not a buglibc++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