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?
Saving the state of a random seed generator
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Saving the state of a random seed generator
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.
Note it's only guaranteed to work within the same major version of Löve.
Re: Saving the state of a random seed generator
Does this mean it’s due for depreciation?
Re: Saving the state of a random seed generator
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:
I don't know if any of these changes came with a change of the internal state format.
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:
And another in 11.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.
(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).wiki wrote:
- Updated love.math.random to have improved numeric distribution.
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
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.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.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
"If each mistake being made is a new one, then progress is being made."
Re: Saving the state of a random seed generator
Oops, I missed that one, sorry.
Re: Saving the state of a random seed generator
Have you tried to use the last number generated as the seed ?
Re: Saving the state of a random seed generator
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
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.
Profile. Do you encounter crashes in LÖVE Android and wanna send me logcats? Please hit me up in LÖVE Discord and send the full logcat file!
Who is online
Users browsing this forum: Bing [Bot] and 17 guests