Replace rand() #390

Closed
opened 2024-08-07 17:36:05 +00:00 by NQNStudios · 2 comments
NQNStudios commented 2024-08-07 17:36:05 +00:00 (Migrated from github.com)

https://github.com/calref/cboe/pull/379#discussion_r1694023035

Seeing this made me realize that we may want to consider moving away from rand() in favour of a Mersenne twister… but that would be something for a separate PR.

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.

https://github.com/calref/cboe/pull/379#discussion_r1694023035 > Seeing this made me realize that we may want to consider moving away from `rand()` in favour of a Mersenne twister… but that would be something for a separate PR. 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.
NQNStudios commented 2024-08-07 17:40:01 +00:00 (Migrated from github.com)

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

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
CelticMinstrel commented 2024-08-07 17:40:23 +00:00 (Migrated from github.com)

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.

The thing to replace it with is probably [`std::mt19937`](https://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine). 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.
Sign in to join this conversation.
No description provided.