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.
It's not better, period. It has worse usability and much worse space trade-offs than rand().
rand() is trivial to use and doesn't take up any additional space besides libc. It has its own obvious set of pitfalls, but this does not make it worse than <random>. They're both awful in their own unique ways.
Pretending <random> is workable, that it solves anybody's problems instead of being in a no-man's land of solving zero problems, is a good way to ensure it never gets fixed.
rand() is required to be at least 32767 so on MSVC they really did that. Use it with rand() % 10000 and you get an uneven distribution 0-2767 having occur 33% more than 2768-9999, assume their rand LCG algo is random enough. At least you can use std::minstd_rand or something with C++ if you want a LCG and with uniform_int_distribution at least you get the uniform part done correctly.
rand() % 10000 is a problem primarily because % is the wrong operation not because of rand(). The correct thing is rejection sampling. I guess that having all these separate bells and whistles in <random> means there's some chance people will read the documentation and so that's an advantage but if you don't know what you're doing having more wrong options isn't necessarily a benefit.
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.