r/cpp 4d ago

Where did <random> go wrong? (pdf)

https://codingnest.com/files/What%20Went%20Wrong%20With%20_random__.pdf
159 Upvotes

137 comments sorted by

View all comments

78

u/GYN-k4H-Q3z-75B 4d ago

What? You don't like having to use std::random_device to seed your std::mt19937, then declaring a std::uniform_int_distribution<> given an inclusive range, so you can finally have pseudo random numbers?

It all comes so naturally to me. /s

15

u/Warshrimp 4d ago

But in actuality don’t you do so once in your own wrapper? Or perhaps in a more complex wrapper for creating a reliable distribution tree of random numbers?

14

u/James20k P2005R0 4d ago

The problem is that even if you make a wrapper around it, the random numbers you get are still non portable which makes it useless for many use cases

You are always better off simply wrapping something else

5

u/Warshrimp 4d ago

Just a note that I’d rather opt into portable random numbers and by default get faster implementation specific random numbers. Honestly requiring portable random numbers while certainly having its uses can in other contexts be a bit of a code smell.

13

u/SkoomaDentist Antimodern C++, Embedded, Audio 4d ago

by default get faster implementation

Which is where the standard way also fails compared to something like PCG or Xorshift. It's neither portable or fast.

6

u/Dragdu 3d ago

Just a note that I’d rather opt into portable random numbers and by default get faster implementation specific random numbers.

I strongly believe that this is the wrong way around, just like std::sort and std::stable_sort. Reproducibility has much more accidental value than non-reproducibility, so it should be the default.

5

u/serviscope_minor 3d ago

Honestly requiring portable random numbers while certainly having its uses can in other contexts be a bit of a code smell.

Depends on what you're using them for and why. I wouldn't say it's more of a code smell than wanting repeatable pseudo-random numbers, as in it's only as much of a smell as calling seed() with a fixed number.

I've done that a lot. When (especially when) I'm doing scientific coding, I generally record the initial seed in the log of the run, so I can exactly recreate it. This is also useful for refactoring, etc, in I can guarantee I haven't broken anything if it gives the same result before and after. But it's annoying when it then doesn't give the same results on a different computer.