5 ms·
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
by b0a04gl 1y ago
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 sequences
it's not a bug in mt19937 itself, it's how random_device (or libc randomness) works differently across environments. makes cross platform tests flaky even when logic is rock solid
>>
std::random_device rd; // might differ per platform
std::mt19937 gen(rd()); // seed depends on rd output
std::uniform_int_distribution<> dist(1, 100);
int random_number = dist(gen); // different on linux vs windows tho same code
- senkora 1y agoWhy 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);
- jhasse 1y agouniform_int_distribution differs between compilers/platforms! Don't use it if you don't want true randomness.
- senkora 1y agoThank 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 https://godbolt.org/z/dd5YMs7zo [2]: https://godbolt.org/z/8svGn7Exc https://godbolt.org/z/8svGn7Exc