Saving the state of a random seed generator

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Madrayken
Party member
Posts: 126
Joined: Sun May 04, 2014 4:21 pm
Location: Oakland CA
Contact:

Saving the state of a random seed generator

Post 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?
Discord: https://discord.gg/tYfHgXc
Bandcamp: https://madrayken.bandcamp.com/
Twitter: @Fluttermind
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: Saving the state of a random seed generator

Post 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.
User avatar
Madrayken
Party member
Posts: 126
Joined: Sun May 04, 2014 4:21 pm
Location: Oakland CA
Contact:

Re: Saving the state of a random seed generator

Post by Madrayken »

Does this mean it’s due for depreciation?
Discord: https://discord.gg/tYfHgXc
Bandcamp: https://madrayken.bandcamp.com/
Twitter: @Fluttermind
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: Saving the state of a random seed generator

Post 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.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Saving the state of a random seed generator

Post 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.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: Saving the state of a random seed generator

Post by pgimeno »

Oops, I missed that one, sorry.
Mokona
Prole
Posts: 2
Joined: Tue Aug 11, 2020 12:24 pm

Re: Saving the state of a random seed generator

Post by Mokona »

Have you tried to use the last number generated as the seed ?
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: Saving the state of a random seed generator

Post 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.
User avatar
AuahDark
Party member
Posts: 107
Joined: Mon Oct 23, 2017 2:34 pm
Location: Indonesia
Contact:

Re: Saving the state of a random seed generator

Post 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.
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!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 83 guests