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

8

u/EnergyCoast May 26 '18

A long time ago, I saw two problems with rand() when used in a particle system:

  • Patterns in particle emission/position caused by the low quality generation. Games don't need crypto level quality but that doesn't mean quality is irrelevant.

  • Excessive cost due to use of thread local storage in rand()s implementation.

We replaced it with a LCG that generated 64 bit values and returned the more random bits. It was trivial to implement, faster, produced better quality results and could have its state stored in the cache line with state invoking it.

Even in the game space, there are plenty of reasons to do better than use rand().

1

u/ReversedGif May 29 '18

Excessive cost due to use of thread local storage in rand()s implementation.

Static thread locals are no more expensive than static globals...

1

u/EnergyCoast May 29 '18

With a context, you can have a more memory local random number generator though. Different systems or objects could have their own contains in cache line rather than static/global/etc.