Hello There, Guest!
View New Posts  |  View Today's Posts
[Info] std::random_device and GCC

  • 0 Vote(s) - 0 Average

01-11-2016, 04:45 AM #1
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

std::random_device and GCC
So I was dealing with a snippet of code for an RNG:
std::random_device rdev;
uint64_t rng_seed = (uint64_t(rdev()) << 32) | rdev();
knuth_lcg rng { rng_seed };

And I realized something: The behavior of this code was not the same when compiled with Microsoft's compiler (cl.exe) vs. my Mingw-w64 installation.

The sequence generated was the same on every run for my executable compiled with g++, but was different everytime when compiled with cl.exe.

Upon further reading, and because std::random_device is a recommended solution if the hardware supports a way to provide a non-deterministic RNG interface, I was a bit surprised to read that GCC hasn't yet implemented it to support such non-deterministic results for Windows at this time. That means that the resultant sequence will be the same -- provided that you're using the same seed over and over from a hardcoded value, which is terrible for those people who do the obvious seed with a single 32-bit value given from std::random_device.


Microsoft on the other hand, has actually supported the proper implementation.

This post was last modified: 01-11-2016, 04:46 AM by AceInfinity.

Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲ ▲

Forum Jump:

Users browsing this thread: 1 Guest(s)