r/cpp • • May 25 '18

Modern C++ Randomness

http://brnz.org/hbr/?p=1783
0 Upvotes

55 comments sorted by

View all comments

7

u/ShakaUVM i+++ ++i+i[arr] May 25 '18

If I'm writing a game or something else where I don't need large random numbers or crypto grade security, I'll just keep using rand until there's a solution in std that is human readable.

29

u/STL MSVC STL Dev May 25 '18

9

u/flashmozzg May 25 '18

If only it wasn't so hard/cumbersome to properly seed std::mt19937 (or so easy to seed it improperly).

10

u/ShakaUVM i+++ ++i+i[arr] May 26 '18

Right. We really need a random number generator in std for humans.

When I watch new programmers make dice rollers, they copy and paste the whole block of code off stack overflow, recreating the engine each time.

2

u/render787 May 27 '18 edited May 27 '18

When I watch new programmers make dice rollers, they copy and paste the whole block of code off stack overflow, recreating the engine each time.

Well, what does that prove? Bad programmers who don't know what they are doing, and copy paste code they don't understand, end up with inefficient code. I don't think a new API is going to change that fact.

The point of C++ is to be able to have low level control over the state of things and not end up with unnecessary initialization etc. The std::random API gives you all the control you need to do this correctly. If you want a "for noobs" API you need to have at least one good programmer on the team who can understand the issues, configure the random generator correctly, and create the "for noobs" wrapper call for everyone else.

Or, post that wrapper code on stack overflow, and everyone can copy it into their include/utilities/ file, and then if there's a problem for your project, someone can dig into it and reconfigure it.

If that code gets baked into standard headers, then no one can fix it for your project. Ruh roh! Lots of bug reports coming, lots of whinging and gnashing of teeth when you have to rip out that function call everywhere.

3

u/ShakaUVM i+++ ++i+i[arr] May 28 '18

What it means is that if the stock way of doing things is too complicated for new programmers to understand, they will copy paste and make it worse than if they'd just stuck to rand() which is easily learned and understood.

1

u/render787 May 28 '18 edited May 28 '18

Good, then what those people should do is go to include/utilities/random.hpp and make a simple inline function that forwards to rand(), and a simple function that forwards to srand, and use this everywhere, until the day when they have big problems with their randomness, and then they can try to redirect this functions to use the std::random API without breaking the API they made for internal use in the project. If they literally just copy paste complex SO code that they don't really understand all over their project, then they are bad programmers and they shot themselves in the foot.

IMO you should always create an abstraction layer over whatever random library you decide to use, so that your code is going to be maintainable. There are lots of problems where e.g. std::random_device just doesn't work on minGW, so pegging yourself to use std::random is setting yourself up for problems if you want to port your code. There are a lot of subtleties in the std::random API -- if you use the standard random distribution objects for instance, they aren't guaranteed to produce byte-for-byte identical results on different platforms, even though std::mt19937 is. So if you are making a multiplayer game or something that needs to have synchronized seeded random sources on different platforms or something, or else you get out-of-sync errors, you might get bitten by this later. It's a very tricky business to generate randomness and I would always assume that whatever lib you decide to use for now you might need to rip it out later, and ensure that I write my code in a way that makes that easy. There's nothing the standards committee or library maintainers can do to change this.

5

u/ShakaUVM i+++ ++i+i[arr] May 28 '18

If they literally just copy paste complex SO code that they don't really understand all over their project, then they are bad programmers and they shot themselves in the foot.

Remember, we're talking about new programmers here. If they knew how to wrap the std functions properly, they could just use them directly.

The std library makes something that should be simple complex, which is the opposite of how it should be. There should be a general purpose random number generator that can be specialized for additional control by people who know what they're doing.

What we have now is a simple and bad option (rand) and a complicated and bad option (random_device). On the balance of things, for new programmers rand is better, because they can at least understand what it is doing.

1

u/render787 May 28 '18

For people who don't really know what they are doing and don't care about the details, why is rand a bad option? How would you want to improve on rand for such people?

Do you just want a newer algorithm than the C88 rand algorithm for the number generation? Or you just don't want to feel like you are using "old, crappy" APIs? If we just make a new rand with the same API and a different algorithm, then in a few years that is going to be an old crappy API also.

Should all of the output of rand be implementation-defined, so that stdlib maintainers can change the algorithm at will? That hardly sounds useful to me, but I guess for newbies who don't really care about the details maybe it's fine?

1

u/ShakaUVM i+++ ++i+i[arr] May 29 '18

Rand is bad for the reasons listed voluminously in this thread. But it is simple to read write and understand so it is better for new programmers.

→ More replies (0)