[15.25]math.random non deterministic

This subforum contains all the issues which we already resolved.
Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

[15.25]math.random non deterministic

Post 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
Attachments
ChoumikoTest_0.0.1.zip
(652 Bytes) Downloaded 121 times
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: [15.25]math.random non deterministic

Post by darkfrei »

What difference between

Code: Select all

local random = math.random
and

Code: Select all

local random = math.random()
User avatar
Nexarius
Filter Inserter
Filter Inserter
Posts: 275
Joined: Sat May 09, 2015 7:34 pm
Contact:

Re: [15.25]math.random non deterministic

Post 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
Rseding91
Factorio Staff
Factorio Staff
Posts: 14253
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [15.25]math.random non deterministic

Post 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.
If you want to get ahold of me I'm almost always on Discord.
Post Reply

Return to “Resolved Problems and Bugs”