Page 1 of 1

Saving the state of a random seed generator

Posted: Mon Aug 10, 2020 10:27 pm
by Madrayken
Hi folks,
I'm writing a turn-based adventure game which uses random seeds extensively.
I'd like to save the state of the random generator when the player saves, so that there's no difference between saving and loading and continuing to play the game.

So far I've been using a horrible table lookup of 2000 random numbers to do this, but there are huge issues with this solution. Anyone else have a favourite method of saving the state of love.math.random?

Re: Saving the state of a random seed generator

Posted: Mon Aug 10, 2020 10:46 pm
by pgimeno
Rather than love.math.random, you can use a RandomGenerator object, which has a RandomGenerator:getState() method.

Note it's only guaranteed to work within the same major version of Löve.

Re: Saving the state of a random seed generator

Posted: Tue Aug 11, 2020 2:22 pm
by Madrayken
Does this mean it’s due for depreciation?

Re: Saving the state of a random seed generator

Posted: Tue Aug 11, 2020 2:40 pm
by pgimeno
No, love.math.random is provided for the convenience of not having to create an object, for those who just want random numbers and no advanced features. It was introduced at the same time as the RandomGenerator object IIRC.

The pseudorandom number generator used internally may change between major versions. The developers make no guarantees that the same generator will be kept from one major version to another; they only guarantee that it won't change between minor or patch versions. And if the random number generator changes, it's likely that the state save format changes as well.

I think such a change happened in 0.10.0:
wiki wrote:
  • Updated love.math.setRandomSeed and RandomGenerator:setSeed to produce better results for the first few random() calls.
  • Updated love.math.random and RandomGenerator:random to produce slightly better results in general.
And another in 11.0:
wiki wrote:
  • Updated love.math.random to have improved numeric distribution.
(note however that I get the same numbers for the same seed in 11.3 and 0.10.2, so it's unlikely that the state format has changed between 0.10 and 11.0).

I don't know if any of these changes came with a change of the internal state format.

Re: Saving the state of a random seed generator

Posted: Tue Aug 11, 2020 7:05 pm
by ReFreezed
pgimeno wrote: Mon Aug 10, 2020 10:46 pm Rather than love.math.random, you can use a RandomGenerator object, which has a RandomGenerator:getState() method.
The equivalent function for love.math.random is love.math.getRandomState. love.math.random simply uses a hidden RandomGenerator object that LÖVE automatically creates at startup.

Re: Saving the state of a random seed generator

Posted: Tue Aug 11, 2020 7:36 pm
by pgimeno
Oops, I missed that one, sorry.

Re: Saving the state of a random seed generator

Posted: Wed Aug 12, 2020 10:54 am
by Mokona
Have you tried to use the last number generated as the seed ?

Re: Saving the state of a random seed generator

Posted: Wed Aug 12, 2020 6:43 pm
by pgimeno
Mokona wrote: Wed Aug 12, 2020 10:54 am Have you tried to use the last number generated as the seed ?
Not a good idea. If the last number generated was in the range 0 to 1, for example, that will only result in two possible sequences after restoring.

That method only works when drawing full-range integers from a generator that consists of a pseudorandom cyclic permutation, and with a state space that fits in the integer (these two conditions typically happen in linear-congruential generators, like those in some old C or Pascal libraries). Löve's built-in generator's state space is 64 bits IIRC, which don't fit in the 53 bits a double can hold.

Re: Saving the state of a random seed generator

Posted: Thu Aug 13, 2020 11:02 am
by AuahDark
If we're gonna change the RNG algorithm in next major release (slime said if he's gonna rework the LOVE RNG, he'll use PCG), maybe we'll allow user to select the RNG algorithm and "pcg" is default (in this case) and "xorshift" to use the older RNG.