CppCon 2016: Cheinan Marks “I Just Wanted a Random Integer!"

CppCon · 21K views · 2016 · 53:57

Open on YouTube

C, C++, Rust, Go & systems languagesSoftware performance & optimizationtalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about C++ random number generation, such as whether to reconstruct distributions, why to avoid rand(), and which generators are fast.

Recommended by 4 people across 4 different months, 2017 to 2023.

What it is

A CppCon 2016 talk by Cheinan Marks of Spiral Genetics on the C++ <random> header. He walks through why generating a simple random integer is complicated, then covers generators like mt19937, the cost of constructing distributions, entropy, and benchmarks of different options.

What people said

Strong rec m_dunaevski · 2017-02 · Stack Overflow · on “String vs vector to store large texts”

since vector and string performance is implementation dependent there is no definitive answer. i suggest that you benchmark these actions in your environment and choose. this [this video] is a very nice lecture about benchmarking your code and how even the standard cannot always suggest the right tools. The punchline of the talk being that performance can change drastically and unpredictably - in one example an instantiation moved inside a loop actually cut running time by 80% by kicking in compiler optimization. if you really care about performance always measure yourself.

Recommends scx · 2017-11 · Stack Overflow · on “what are some optimization tricks to make my code run faster”

Cheinan Marks has some benchmarks and performance tips related to random generators & friends in his cppcon 2016 talk I Just Wanted a Random Integer! [this video] He mentions some fast generators as well IIRC. I'd start there.

Recommends Casey · 2021-04 · Stack Overflow · on “Re-initializing random distribution”

I essentially do what your first implementation does. Construct one every time I need a distribution. That said, yes, the behavior of the different distributions are guaranteed by the standard [LINK] to behave in specific ways. STL recommends [LINK] , that since distributions are relatively cheap, don't worry about constructing one every time you need one or need a new range. He also says if you don't want to construct one every time, you can use the param [LINK] member function to change the distribution range. Microsoft Channel9 link if the above direct Youtube link dies (seek to 30 minutes in): [LINK] EDIT I was re-watching a CppCon talk from a few years ago that discusses this exact ques

Recommends Pepijn Kramer · 2023-10 · Stack Overflow · on “How to fast convert uniform random integer to floating point value with equal number of positive and negative values”

Read this documentation : [LINK] on how to generate random numbers (in any kind of distribution). Don't use old style "rand()". Want to know why watch All I wanted was a random number" [this video]