Replace rand() #390
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
https://github.com/calref/cboe/pull/379#discussion_r1694023035
I was thinking back over this, and remembered a practical/important reason why we should stop using
rand()for randomness.The code uses rand() for gameplay randomization, but it also uses rand() to generate unique ids for ui controls. The replay system needs gameplay randomization to be totally deterministic, and so far it hasn't mattered that the UI system is also calling rand(), but if we ever change a dialog xml in a way that the UI system needs to call rand() more or less times, then game replay determinism will be broken between old and new versions of cboe, which defeats the core purpose of replays.
So at the very least we need to use a RNG system where the UI controls' randomized ids are not interfering with the same RNG the gameplay logic uses.
I didn't know this until just now, but C++11 provides a Mersenne twister implementation so this should actually be pretty trivial. https://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
The thing to replace it with is probably
std::mt19937. With this there could easily be separate instances for UI and gameplay stuff. Maybe a third instance if gameplay code uses it for something cosmetic somewhere.