Determinism and being mindful of RNG

(Karll) #1

Continued from

   self.swarm_timer -= delta_seconds;
    if self.swarm_timer <= 0.0 {
        let mut swarm_entity_builder = lazy_update.create_entity(&entities);
        let mut rng = thread_rng();

Not really a review concerning the code, but sounds like a good opportunity to raise this point: we have to be mindful about RNG.

One good feature to have is determinism, this allows us to repeat the same simulation with the same results, it makes testing easier, a player can easily share his simulation by sharing the initial seed (like Minecraft) and tweaking becomes more fun because players can run the same simulation but tweak a few things and see how it changes.

From the thread_rng() docs:

After generating a certain amount of randomness, the RNG will reseed itself from the operating system or, if the operating system RNG returns an error, a seed based on the current system time.

The internal RNG used is platform and architecture dependent, even if the operating system random number generator is rigged to give the same sequence always. If absolute consistency is required, explicitly select an RNG, e.g. IsaacRng or Isaac64Rng.
It seems that thread_rng() isn’t deterministic at the very least between different platforms.

Of course that’s open to discussion, do we really want this Feature? And there is no reason to change until we decide this and roll a deterministic RNG that suits our use case.

1 Like

(Marco Fruhwirth) #2

+1 for using a resource for rng. The only problem would be that systems can’t access rng at the same time since it needs to be mutable.

1 Like

(Erlend Sogge Heggen) #3

Aye we do! But making it optional could be interesting.


(Lokathor) #4

The RNG should always be an explicit part of the program state. Using the thread_local RNG is fine for basic scripting and not much else.

Erland is correct though: while some people want to have a fully deterministic game (“daily challenge” world generation and such), there’s just as many people who will tell you that your game is broken if the RNG is in the save data and they load the game just before an attack and the attack plays out the same as last time over and over. So you’d probably want a “save scum” option when loading a game or something obvious so the player can know what’s going on with that.