I want to be able to store the state of a RNG so I can go set forwards and backwards through a list of seeds. I thought I could just pull the seed from a generator, like this (which runs if you just stick it in a directory as main.lua and type "love ." from a prompt):
I can think of some reasons why you would want the state of the rng saved.
For example: you have a slot machine minigame. The player can save before playing the slots, then reload if he doesn't win and try again, until he wins a million points (I know I did this in GTA SA). But you don't want that to happen. You want to have the exact same outcome all the time whenever the player saves and reloads, so he can't use this 'cheat'. But you still want the slot machine to be random, so how do you know, after having generated an arbitrary number of previous random numbers, what the next random number is going to be? Only by knowing the rng's state at the time of the save.
If you want to do that, you need to store the initial seed and the amount of numbers generated so far, and you can "rewind" the number generator to wherever, by simply setting the same seed and then calling the generator that many times. It'll end up in the same state it was in before.
(If you do this for really long games, considering occasionally resetting your RNG with a new seed so that you don't have to call it a million times when the game loads in order to reset it.)
Sorry; I was away for a while for family stuff. I'm using this for a game where the levels are randomly generated. I want to be able to reverse the state throughout levels. I was able to hack this together, and it avoids generating tons of random numbers between state changes (unrelevant code cut out):
function infiniteLevelTable()
local levelTable = ...
...
local initSeed, ... = 1, ...
local rngHistory
local newRng
local generateLevel = function ()
physicsClean(levelTable.levelBlocks); collectgarbage("collect"); levelTable.levelBlocks = {}
levelTable.levelBlocks, newRng = generateBlocks(rngHistory[#rngHistory],rndintrv,slope,minheight,maxheight,length) end
...
levelTable.gotoFirstLevel = function () levelTable.level = 1; rngHistory = {love.math.newRandomGenerator(initSeed)}; generateLevel() end
levelTable.gotoNextLevel = function () levelTable.level = levelTable.level + 1; table.insert(rngHistory, newRng); generateLevel(); return true end
levelTable.gotoPreviousLevel = function () if levelTable.level ~= 1
then levelTable.level = levelTable.level + 1; rngHistory[#rngHistory] = nil; generateLevel(); return true
else return false end
end
return levelTable
end
This is effectively what I mentioned about occasionally resetting your RNG, except in a more controlled fashion It should work just fine.
Although your gotoPreviousLevel is currently removing the value for the current level, is this intentional? It'll generate a different level when you return to the level again.
Right now it seems to be deleting the seed from the rngHistory table though, if I'm reading this right. So if you go back to the next level again, there won't be a value there, and a new one will be generated.