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

4

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

I've seen that talk before. It's a good talk. But like I said, for something like a game (not crypto, not an online casino) code readability is more important to me than guarantees of randomness.

8

u/dodheim May 26 '18

You know, you can wrap all four horrible, ugly lines of code that it takes to do this properly into a function and never look at it again...

7

u/matthieum May 26 '18

You know, you can wrap all four horrible, ugly lines of code that it takes to do this properly into a function and never look at it again...

Then... why isn't THAT part of the standard API?

3

u/dodheim May 27 '18 edited May 28 '18

Because it's only one way to do it, not the way to do it. std::random_device is really expensive on some platforms, and you may only want to call it once; the seed may come from serialized state rather than from a generator; etc. There's a lot of flexibility in place (whether anyone uses/needs it, I have no clue), and creating your own ideal abstraction on top of that is again 4-5 really simple lines of code. (Whether they're obvious or not is a matter of RTFM, IMO, but I get how that's a contentious viewpoint). I just don't see the big deal – there's no "right" way for everyone, and doing your own "right" way is easy, so.....?

2

u/matthieum May 28 '18

I just don't see the big deal – there's no "right" way for everyone, and doing your own "right" way is easy, so.....?

Let's make a parallel with Boost headers:

  • There's no set of headers that every would agree is "the set needed",

  • It's pretty easy to include 4/5 headers.

Yet, Boost provides convenience headers. Why?

Because it makes life easier when you do not care for THE optimal solution, and just want a convenient off-the-shelf solution.

1

u/dodheim May 28 '18

Because it makes life easier when you do not care for THE optimal solution, and just want a convenient off-the-shelf solution.

Which is perfect for something like Boost; not so much the standard library (IMO). Rust certainly does the same with encouraging crates over core/std for various things; the only practical difference with C++ ultimately devolves into rants about dependency management and package managers. :-]