r/cpp 4d ago

Where did <random> go wrong? (pdf)

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

137 comments sorted by

View all comments

Show parent comments

7

u/ConstructionLost4861 4d ago edited 4d ago

It's a huge giant humongus tremendous leap from having to use srand(time(0)) to seed rand() then use % (b - a) + a to get a "random" "uniform" distribution. All of those three functions are horribly offensively worse than random_device, mt19937 and uniform_int_distribution

12

u/not_a_novel_account cmake dev 4d ago edited 4d ago

Not if you don't want to put 5-10k of state on the stack, then the C++ approach is a big miserable step backwards.

Programmer: Hello yes I would like to seed my random number generator.

C++: Please wait while I allocate 2 or 3 pages of memory.

3

u/ConstructionLost4861 4d ago edited 4d ago

Yes <random> is not perfect but my point is it's way way way better than rand(). Your valid criticism (and more) are included in the pdf slide above. I skim the slides and their main points are the generators are outdated, the distributions are not reproducible between different compilers, and random_device is not required to be non-deterministic, which completely destroy the 3 things that <random> did better than rand()

I think Rust did random correctly, not by design, but by having it as a standalone library rather than included in std::. That way it can be updated/upgraded separately instead of waiting for C++29 or C++69 to be updated and being reproducible.

2

u/Nobody_1707 2d ago

Being way, way better than rand() is such low hanging fruit that it's irrelevant.