Page 1 of 1

[15.25]math.random non deterministic

Posted: Fri Jun 30, 2017 10:19 am
by Choumiko
Luas math.random seems to be non deterministic depending on how you call it inside your scripts:

Code: Select all

local math = math
local random = math.random

local random1 = function()
    game.print(random())
end

local random2 = function()
    game.print(math.random())
end

commands.add_command("test_random1", "", random1)
commands.add_command("test_random2", "", random2)
/test_random1 desyncs every time, whereas test_random2 never desyncs

Related mod bug report + commit that solved it: https://github.com/Choumiko/FARL/issues/85

Mod to reproduce the desync attached

Re: [15.25]math.random non deterministic

Posted: Fri Jun 30, 2017 10:25 am
by darkfrei
What difference between

Code: Select all

local random = math.random
and

Code: Select all

local random = math.random()

Re: [15.25]math.random non deterministic

Posted: Fri Jun 30, 2017 10:36 am
by Nexarius
math.random is just a variable (variables can be functions)
math.random() is calling the function with the name math.random
darkfrei wrote:

Code: Select all

local random = math.random
The variable random is set to math.random
darkfrei wrote:

Code: Select all

local random = math.random()
The variable random is set to the result of the function math.random

Re: [15.25]math.random non deterministic

Posted: Fri Jun 30, 2017 11:02 am
by Rseding91
Thanks for the report. This was due to the order that we replace math.random with our own deterministic random generator. The control.lua contents are parsed first then we replaced math.random. I changed it for the next version of 0.15 so it replaces math.random first then parses the script so both functions will get a reference to our built in deterministic random generator.

It's fixed now for the next version of 0.15.