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

preview_player
Показать описание


I just wanted to generate a random integer between 1 and 100 for my unit tests.

An hour later I was down the rabbit hole of the C++ <random> header wondering why it was so complicated, weren't rand() and modulo good enough and was <random> overkill, genius or both?

Several hours later I had watched STLs 2013 Going Native talk on < random> (go watch it!), read some blogs and had even more questions: does the mt19937 generator go on the heap? Just how cheap are uniform distributions to construct? Were there even better generators out there and were they easier to use? Oh and what is entropy? How do you put it in a pool and how can it be exhausted?

The story took a few unexpected twists and turns, but in the end I got my random integers, and answers to almost all my questions which I will share. Whether <random> is genius or overkill though, you will have to decide for yourself.

Cheinan Marks
Spiral Genetics
As an engineer with a chemical engineering background, Cheinan is interested in practical code and wants to take the latest advances both in science and programming and use them in real life working code along with applying engineering principles to software development. Cheinan is a senior developer at Spiral Genetics, Inc. in Seattle.


*-----*
*-----*
Рекомендации по теме
Комментарии
Автор

just in case anyone cant understand, he's referencing Stefan T. Lavavej's "rand() Considered Harmful" (2013) talk.

narnbrez
Автор

I swear to god there's a con curse, NEVER comment on the determinism of the presenters computer

disk__
Автор

Almost everyone initializes their engine by a single integer from random generator. Mersenne twister has huge state and almost everybody initializes it with single integer, that gives you only 2^32 different sequences. Correct usage is to gather enough "true" random bits into vector or array and pass this data through seed_seq like interface into mersenne twister constructor.

MarekKnapek
Автор

Great talk, looking forward to his analysis on the loop-constructor anomaly!

funkmasterhexbyte
Автор

at 43:00, I suspect that the speed killer is the DIVIDE operation. Moving that out as a compile-time constant will avoid doing that. Division not only takes a lot of cycles, but it demands a lot of the internal queues and resources, so it inhibits superscaling. On my blog I have a post where I showed how replacing a divide by a bunch of other code made it faster.

JohnDlugosz
Автор

I think minstd_rand uses a modulo operation to generate numbers, which might be why it’s slower than the mersenne twister. The mersenne twister does not use modulo operation.

suzannesmith
Автор

I can't find the blogspot he mentions at 4:45. I can hear the author of the blog is Ben Deen (perhaps I am wrong, please correct me). Please, if you know the reference give me a hand. Many thanks

pablo_brianese
Автор

I really hate it when people show code in a variable-width font.

Fetrovsky
Автор

Please don't avoid re-initializing an rng by making it static, now your code suddenly isn't thread safe. Either put it in a thread-local variable, or if you need more control, pass references and be careful (or use Rust). Also, +1 for PCG instead of MersenneTwister; xoshiro is also great.

WitherBossEntity
Автор

Entropy is not the energy/work you can get out of a system. That's Enthalpy. Entropy is the amount of disorder/chaos in a system that is responsible for the energy/data you cannot get out of a system. This is why the term is used when it comes to randomness as entropic chaos is responsible for unpredictability in a system.

rbledsaw
Автор

This unfortunately repeats some myths about /dev/urandom. It is not meaningfully “less random” than /dev/random from a cryptographic perspective, meaning the fact that it blocks is only a disadvantage.

As a corollary, essentially any modern non-embedded system will never “run out” of randomness, and especially not to the point where it should throw. In my experience, and in ≈all the code that I’ve seen, that should be treated as a catastrophic, unrecoverable failure.

PaulFisher
Автор

Here's another hint: If you need randomness in unit testing, you're probably doing property-based testing, and should just use a property-based testing framework. Interesting talk though.

jackgenewtf
Автор

Hopefully nobody will use the inner loop example. It was just an example, but he did not mention, that what is the pitfall of the example. This was just for perf test.

nwhous
Автор

Duckduckgo is still being use confirmed

lunedefroid
Автор

Why is random number generation in c++ such a shit show?

ArthurSchoppenweghauer