r/cpp • • May 25 '18

Modern C++ Randomness

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

55 comments sorted by

View all comments

Show parent comments

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?

2

u/render787 May 28 '18

Because then if you find out later you need to use a different RNG, you cannot without rewriting all your code or patching the stdlib?

IMO you should write your program expecting that you may need to change to a different RNG at some point. There's no way you can be sure that you won't.

1

u/matthieum May 28 '18

IMO you should write your program expecting that you may need to change to a different RNG at some point. There's no way you can be sure that you won't.

KISS & YAGNI disagree with you :)

  1. Many programs do not change their PRNG (any introductory/write-once program falling in this category).

  2. Many programs do not expose their PRNG (only what is generated), and therefore switching the PRNG is a few lines of code.

As anecdotal evidence, in 10 years of professional development, I have never needed to change a PRNG.

Preparing for changing the PRNG would have been a case of over-engineering in all the programs I've ever hacked on.

2

u/render787 May 28 '18

Ok, then just use rand I guess?

For tiny programs, it doesn't matter

1

u/matthieum May 28 '18

In terms of interface, rand.

In terms of implementation, a thread-local PRNG with relatively good characteristics would be neat.

1

u/render787 May 29 '18

I guess there's nothing too terrible about that.

Is there anything we could do to make sure that if we added another rand with slightly different characteristics that is meant to be "easy easy easy", we don't have the same conversation again in 5 years because of perceived drawbacks of such a PRNG?

Would you want the output of this interface to be standardized and portable, or just the broad characteristics of the algorithm? Would you want to allow stdlib maintainers to change it at will, since no one will be using this unless they don't care too much about the details?

→ More replies (0)

1

u/ClaymationDinosaur Jun 01 '18

I used to be happy with this as well; for program that didn't matter. But in my experience, people execute as they learned, and using rand() for learning in throwaway programs means people end up using it when it matters as well.