Live data from Hacker News

C++ Seeding Surprises (2015)

pcg-random.org

21–26 of 26 posts

Re: C++ Seeding Surprises (2015)

#21
post #13

found one more flakiness over cross platform, when seed mt19937 same way on linux and windows, same compiler, same code... but problem is std::random_device or libc internals differ under the hood. some platforms do random_device as true hardware entropy, others fake it or seed from diff system sources. so seed retrieved isn't stable cross platform. that means mt19937 starts from diff states, causing different random…

Why not just run your tests with a fixed seed? e.g.

    std::mt19937 gen(42);
    std::uniform_int_distribution dist(1, 100);
    int random_number = dist(gen);

Re: C++ Seeding Surprises (2015)

#22
post #5

This talks about 'bad' seeding quite a lot. But it really depends on what you need what is bad and what is good. Sometimes you need to have a reproducible program so you need to write the random number generator yourself and/or otherwise fix the algorithm. Then you can use '5' as the seed. This is quite often good enough for simulations. Sometimes you want to create cryptographic randomness. Then you need to somewher…

I don't think it disputes that sometimes using a fixed small number is a good seed for reproducibility. The point is just, if you do happen to be in the other camp, where you actually want to populate the full state of the RNG using hardware randomness, C++ does not give you an obvious way to do that.

Re: C++ Seeding Surprises (2015)

#23
If you want reproducibility and statelessness, there's random123 which I highly recommend. Particularly useful for GPU code.

(Not cryptographically secure, but passes all statistical tests you throw at it).

Re: C++ Seeding Surprises (2015)

#24
post #12

Earlier quoted context omitted.

Well, it's in std. So there's an appeal to authority (the C++ language authors should be smart, right?) and convenience.

There are still others in . Yet they always use the cool twister one with the funny numbers

There's std::linear_congruential_engine, but it doesn't provide you with any (good) default parameters; only the bad ones from historical rand() implementations (minstd_rand0 / minstd_rand).

Re: C++ Seeding Surprises (2015)

#25
post #21
post #13

found one more flakiness over cross platform, when seed mt19937 same way on linux and windows, same compiler, same code... but problem is std::random_device or libc internals differ under the hood. some platforms do random_device as true hardware entropy, others fake it or seed from diff system sources. so seed retrieved isn't stable cross platform. that means mt19937 starts from diff states, causing different random…

Why not just run your tests with a fixed seed? e.g. std::mt19937 gen(42); std::uniform_int_distribution dist(1, 100); int random_number = dist(gen);

uniform_int_distribution differs between compilers/platforms! Don't use it if you don't want true randomness.

Re: C++ Seeding Surprises (2015)

#26
post #25
post #21

Earlier quoted context omitted.

Why not just run your tests with a fixed seed? e.g. std::mt19937 gen(42); std::uniform_int_distribution dist(1, 100); int random_number = dist(gen);

uniform_int_distribution differs between compilers/platforms! Don't use it if you don't want true randomness.

Thank you, that makes sense. I can see that being an issue for testing.

I wouldn't describe "pseudo-random sequences that differ between compilers/platforms/versions but are deterministic for a given toolchain" as "true randomness" but I understand the point.

I have confirmed that uniform_int_distribution gives different results on x86-64 clang (trunk) with libc++ [1] vs x86-64 clang (trunk) with libstdc++ [2].

[1]: https://godbolt.org/z/dd5YMs7zo

[2]: https://godbolt.org/z/8svGn7Exc

Post reply on HN