CppCon 2016: Walter E. Brown “What C++ Programmers Need to Know about Header <random>"
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about srand, seeding, reproducibility and distributions, as the authoritative guide to using <random> properly.
Recommended by 3 people across 3 different months, 2017 to 2018.
What it is
A CppCon 2016 talk by Walter E. Brown, the principal author of C++'s <random> header. It covers the header's conceptual design, the correct usage pattern, the common rand()/srand()-style anti-pattern, and toolkit ideas built on its facilities.
What people said
Strong rec SU3 · 2017-02 · Stack Overflow · on “Does it matter if I don't explicitly cast time_t argument to unsigned int argument when using srand with time?”
time_t [LINK] is a typedef for an implementation specific (almost always) integral type. Since srand [LINK] takes an unsigned int argument, and any integral type is implicitly convertible to unsigned int , you get exactly the same behavior with or without the explicit static_cast<unsigned int> . If you have c++11 available, I would strongly suggest using the facilities provided in the [LINK] header for any serious random number generation. For a thorough discussion of this, I suggest watching [this video] Walter Brown's talk at CppCon 2016.
Recommends Jonas · 2017-07 · Stack Overflow · on “Create random number with exponential distribution in c++ (visual stduio)”
The problem is that you are calling packetArrivalRate with generator are the parameter, when the function expects a double . Said function return a exponential_distribution<> which needs a generator to give you the exponentially distributed numbers. One simple (and very ugly) way to solve this is to give packetArrivalRate a proper argument (note that this argument is completely ignored by the function, so its value doesn't matter), and then call operator() on the return value: double time = packetArrivalRate(0)(generator); Alternative design A more reasonable way is to not ignore the argument, and then provide rnd : double time = packetArrivalRate(rnd)(generator); And define packetArrivalRat
Recommends super · 2018-06 · Stack Overflow · on “If I use the same seed on every platform, will the random results be the same?”
For rand there are no such guarantees. For the <random> header however things are different. On cppreference [LINK] it states All of the engines may be specifically seeded, serialized, and deserialized for use with repeatable simulators. You can serialize and deserialize the state of an engine with the operator>> and operator<< and send this state over network and be guaranteed reproducability over different platforms. This is mandated by the standard. std::default_random_engine may use any engine under the hood, and therefor has no such guarantee. Distributions are implementation defined, and you would need to define your own to keep the guarantee of reproducability. Bonus video link [VIDEO