Page 1 of 1

Storing Seed and State data from random generator

Posted: Sat Dec 26, 2015 2:22 am
by Zulsorai
I'll start off by saying that I haven't used Love in a while, recently on my winter break I decided to mess around with it some more.

Currently I am working on creating randomly generated maps. My problem right now is that if I want to recreate a layout I need both the seed and the state of the random generator. However I was hoping to find a way to compress it down to only a few characters so that the user could easily input the data and share with others if they come across something interesting. I've been unsucessful in finding something to suit my needs. I was hoping someone would be able to link me to an article or point me in the right direction.

I've added my .love if you want to take a look at what im working with right now. I know it's not pretty just something I threw together thinking about this.

Edit: Fixed the .love

Re: Storing Seed and State data from random generator

Posted: Sat Dec 26, 2015 3:39 am
by pgimeno
You're using newRandomGenerator with a seed; just display it if you want to be able to enter it later.

You can use :getState() on a RandomGenerator object to obtain a string that represents the state. In the current version that string happens to be printable and short, but I wouldn't rely on either for future versions.

I've made this simple test:

Code: Select all

rng = love.math.newRandomGenerator(1)
s = rng:getState()
--print(s)
for i = 1, #s do
  io.write(string.format('%02X', s:byte(i)))
end
print()
If I uncomment print(s), it says 0x0000000000000001; either way it prints 307830303030303030303030303030303031 which may be the string you're after (you'll have to convert it back from hex to string when the user inputs it). If you want to stick to the current version then you can just use the output of getState() directly.

By the way, your .love file does not have main.lua in the top level, so it doesn't immediately run. It's also a 7z file which I'm not sure how well is supported under Windows. Works fine with Linux except for the top level problem.

Re: Storing Seed and State data from random generator

Posted: Sat Dec 26, 2015 4:16 am
by Zulsorai
What I was thinking of is to find a way to compress the random generator's seed and the hex code for the state down to 8 or 10 digits somehow so that it can easily be shared with other, but I'm quickly thinking its not going to be possible.

I had something like the Binding of Isaac Afterbirth seeds in mind. All the user would have to input would be WQ1G 9T3V and it would fully recreate the floor and all the RNG for it.
Currently I have 1451103084 for a seed and 0xc8839ef0b4d41ee as a state and I need both to recreate the layout.

Re: Storing Seed and State data from random generator

Posted: Sat Dec 26, 2015 5:08 am
by pgimeno
os.time() gives you a 33 bit integer (32 if you don't plan your game to be around in 2038). That can be compressed to 8-9 hex digits or 6 base64 digits.

A 64-bit integer can be compressed to 11 base64 digits or 10 base85 digits. I know there are base64 and base85 libraries for Lua. Base85 has the problem of mixing case and including symbols. Base64 has the problem of mixing case.

With the digits 0-9 A-Z you need 13 characters to encode a 64-bit number, or 7 to encode a 32- or 33-bit number. You'll need to write your own encoding/decoding library, though.

Edit: Rereading your reply I got the impression that you think you need to save both the state and the seed. You don't. The seed is able to reproduce the same sequence. The state is useful if you want to start at any given point of the sequence.

Re: Storing Seed and State data from random generator

Posted: Sat Dec 26, 2015 5:20 am
by Zulsorai
Thanks for the info, this was more of what I was thinking of I'll look into getting this set up.

Re: Storing Seed and State data from random generator

Posted: Sat Dec 26, 2015 9:20 am
by bobbyjones
As long as you only use the random number generator for the world generation it will always be the same no matter what. As long as you use the same seed. But if you use the same rng for other things then you could possibly mess things up. So make sure you have a separate rng for everything.