Repeatable seeded pseudorandomness

Requests or suggestions for new features
Post Reply
RobAtLab
Posts: 48
Joined: 10 Jan 2018, 17:49

Repeatable seeded pseudorandomness

Post by RobAtLab » 14 Nov 2018, 18:36

If one wants to insert deliberate randomness into a simulation using (for example) CompassAngleToDriveAlong=math.random(-180,180) but one wants the series of directions chosen by this function to be random but repeatable then in lua one would typically set math.randomseed(KnownConstantValue) beforehand. When one wanted a different random sequence in a future run of the simulation one would change the constant's value. But I am having trouble doing this. V-REP seems to automatically call something like math.randomseed( os.time() ) which causes the random seeds to be wiped over and non-repeatable randomness to return. When I run simulations where I do not use any randomness in the robot controllers I find them perfectly repeatable each time I run them, while true randomness may be used in the physics engine somewhere the results still end up broadly the same for my types of situation.

My understanding is that V-REP seems to use the same randomness to power the physics engine and to satisfy any calls of math.random(lower,upper) used in controllers.

Can anything be done in a future release such that setting math.randomseed(KnownConstantValue) within the controller (non-threaded) child scripts of each robot (each robot that uses math.randomseed anyway) in the scene during the initialisation step leads to repeatable randomness. If one uses multiple similar robots and wants them to have independent randomness one could just give them each a different seed, but when you ran the same sim with the seed you would have certainty that ,except for little differences tyhat may result in the physics engine calculations, one would get the same random sequences and repeatable results. In the swarms/modular/evolvable robotics fields we often like to run loads of repetitions of a scenario with a different random seed each time, but with the knowledge that if we see something intriguing happen for a specific random seed value we can go back to that random seed value and get exactly the same sim to run again. Things are a bit tricky when the lack of a way to set random seeds as one either has to go through the effort of creating a random sequence in an external text file for controllers to read from (and get the same random sequence each time unless you change the file contents) or one can do genuine randomness but not be able to repeat a particularly interesting experiment to mroe carefully investigate why a certain result happened.

Thanks

coppelia
Site Admin
Posts: 6867
Joined: 14 Dec 2012, 00:25

Re: Repeatable seeded pseudorandomness

Post by coppelia » 16 Nov 2018, 11:44

Hello,

you are perfectly right: even if the individual script states are completely isolated from each other, they still share the same random number generator. This is currently problematic and the only workaround solution I see is to generate several sequences of random numbers (each time with a different seed), and store those sequences in one or several files. Then, each script should first select the file that corresponds to a given seed, then simply sequentially read the file content. This way, a same seed will produce the exact same number sequence in your different scripts.

In next release, there will be a specific random number generator attached to each script, and you will be able to use following:
  • sim.getRandom() --> returns a random number between 0 and 1
  • sim.getRandom(seed) --> sets a new seed and returns a random number
Alternatively, you will be able to call math.random2 and math.randomseed2 in the same way as you can call math.random and math.randomseed currently (the difference is that math.random2 and math.randomseed2 internally both use sim.getRandom.

Cheers

RobAtLab
Posts: 48
Joined: 10 Jan 2018, 17:49

Re: Repeatable seeded pseudorandomness

Post by RobAtLab » 21 Nov 2018, 18:50

Thank you for that, any idea when the new version of V-REP with this feature will be released?

P.S. I'm assuming you'll do all you can to maintain compatibility of scripts and models between the current version and that next one. I'd hate to have to redefine all my geometry, actuators and custom sensors to get access to the nice random seed functions you're planning on adding.

coppelia
Site Admin
Posts: 6867
Joined: 14 Dec 2012, 00:25

Re: Repeatable seeded pseudorandomness

Post by coppelia » 23 Nov 2018, 06:47

The next release should be out in December-January, hopefully!

Cheers

RobAtLab
Posts: 48
Joined: 10 Jan 2018, 17:49

Re: Repeatable seeded pseudorandomness

Post by RobAtLab » 23 Nov 2018, 13:42

Just to check, will there be a way to set a seed for the whole scene, then even if multiple identical robots are all running the same controller they won't all get the same sequence of random numbers? Will there also be other options to have a separate randomness source and separate randomseed on each robot, in cases where one might want multiple robots to all be random but in the same way?
Thanks

coppelia
Site Admin
Posts: 6867
Joined: 14 Dec 2012, 00:25

Re: Repeatable seeded pseudorandomness

Post by coppelia » 23 Nov 2018, 15:08

The default random function of Lua (i.e. math.random) is using the same C API function as a few other random numbers that are needed within V-REP. So that would not represent a predictable random number sequence, even if seeded.

On the other hand, you will always be able to call the same script function (that uses the math.random2 function) from other scripts, if you need a same, seedable and predictable random number generator. For that use sim.callScriptFunction upon always the same script.

Cheers

Post Reply