Generating predictable sequences of random numbers with math.random() and math.randomseed()
|Opened on:||Friday, November 7, 2014 - 16:34|
|Last modified:||Saturday, March 9, 2019 - 18:04|
I have a Recipe that often uses math.random() to generate random numbers. To make it give a different sequence of random numbers each time, I use math.randomseed(os.time()) first. If I want it to always give the same sequence of random numbers, I use math.randomseed(rand_seed), where rand_seed can be input from a dialog box, and the same value for rand_seed gives the same sequence of random numbers.
Sometimes the Recipe runs a while and then quits, and I would like to re-start the Recipe so it begins where its last run left off. Is there a LUA command that will output a value for rand_seed that you can use in math.randomseed(rand_seed) to continue the sequence of random numbers from where it left off? I envision having my Recipe output appropriate values for rand_seed as it runs, so that when it quits, I just find a recent reported value for rand_seed and input that in my next run of the Recipe.
One workaround I have tried is to have a variable called rand_so_far that counts how many times math.random() has been called since math.randomseed(rand_seed) was used. I report the value of rand_so_far regularly. Then if the Recipe quits, I can input the same value of rand_seed with the latest reported value of rand_so_far to continue the sequence of random numbers. I just have a loop after I execute math.randomseed(rand_seed) that calls math.random() rand_so_far times. The problem with this workaround is that I suspect some Foldit commands call math.random() themselves without adjusting rand_so_far appropriately, and this throws off the sequence of random numbers.
Does anyone have any suggestions?