Generating predictable sequences of random numbers with math.random() and math.randomseed()

Case number:845813-1998681
Topic:Game: Tools
Opened by:jeff101
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?


(Fri, 11/07/2014 - 16:34  |  6 comments)

spvincent's picture
User offline. Last seen 1 day 4 hours ago. Offline
Joined: 12/07/2007
Groups: Contenders

You could write your own linear congruential random number generator (which can be done in a couple of lines of code) and then use the rand_so_far technique you mention.

Joined: 07/21/2013
Groups: Beta Folders

There is already a random number generator in Lua on Foldit:

Name: Random Number Generator
ID: 2634
Created on: Wed, 06/02/2010 - 04:35
Updated on: Fri, 08/13/2010 - 21:07

Run this script and it will print random numbers to the output window. This is not meant to be a useful script by itself, but instead is a starting point for other scripts that need to do some randomization. It needs a seed, so it uses the least significant digits of the current score. If you rerun the script with no changes to the structure, it will generate the same sequence. Any change to the structure will show a different sequence. Feel free to use in your own scripts with credit and the copyright notice intact. See the comments in the script for more information.

jeff101's picture
User offline. Last seen 3 days 1 hour ago. Offline
Joined: 04/20/2012
Groups: Go Science

It would be great if Foldit or LUA had a function like Matlab's rng for random number generation
(see for details).

In Matlab, s=rng returns the present State, Type, and Seed for the random number generator.
Later you can use rng(s) to restore the State, Type, and Seed to their previous values.

Is there anything like this in Foldit or LUA?

Thanks again!

brow42's picture
User offline. Last seen 2 weeks 4 days ago. Offline
Joined: 09/19/2011
Groups: None

There is not. Lua uses the C library for many things in os and math. Lua doesn't have a version that fetches the state. You don't even know which RNG you are getting (Unix rand, random, lrand48, DOS rand) so it's platform dependent. Tlaloc implemented MT for the sole use of getrandomseed, but you can't get to that state (which is huge).

Here's a quote from lua-users mailing list: "Lua relies on rand from libc. It must not be too clever about what it does with the result of rand lest it breaks its randomness (which may be weak already)."

I haven't looked at the script mentioned above, but that's what you want: an RNG implemented in Lua.

jeff101's picture
User offline. Last seen 3 days 1 hour ago. Offline
Joined: 04/20/2012
Groups: Go Science

Thanks for the info, everybody.

@brow: When you say "I haven't looked at the script mentioned above", what script do you mean? MT? rand from libc?

Usually we want different sequences of random numbers each time we run a Foldit Recipe, but for puzzles like where we seek ways to reliably crash Foldit, it helps to have repeatable sequences of random numbers.

Wipf's picture
User offline. Last seen 2 years 16 weeks ago. Offline
Joined: 10/06/2018
Groups: Go Science

I'm sure brow is refering to tlaloc's recipe 'Random Number Generator' linked above by brgreening.


Developed by: UW Center for Game Science, UW Institute for Protein Design, Northeastern University, Vanderbilt University Meiler Lab, UC Davis
Supported by: DARPA, NSF, NIH, HHMI, Amazon, Microsoft, Adobe, Boehringer Ingelheim, RosettaCommons