Page 1 of 1

[Solved] Drawing a blank - math.random

Posted: Thu Jul 28, 2016 6:24 pm
by TheSAguy
Hi,

I'm just not able to figure this out.
I want to have a percentage chance of something happening based on the evolution factor.
So at 1% the chance if it triggering would be 1% and at 100%, a 100%.

I have

Code: Select all

local attack_chance = math.random(100)
if attack_chance < math.floor(game.evolution_factor*100) then
--- something happens
end
Does that look right?

Re: Drawing a blank - math.random

Posted: Thu Jul 28, 2016 6:33 pm
by mojo2012
Hi, somewhere I read that math.random and multiplayer domt go too well together ... just as a sidenote

for 1% chamce you should probabely do so,ething like evolution factor *0.1

Re: Drawing a blank - math.random

Posted: Thu Jul 28, 2016 6:49 pm
by TheSAguy
What would you recommend using besides Random.Math for MP then?

Re: Drawing a blank - math.random

Posted: Thu Jul 28, 2016 7:19 pm
by daniel34
mojo2012 wrote:Hi, somewhere I read that math.random and multiplayer domt go too well together ... just as a sidenote
Using math.random is perfectly fine, even in multiplayer. It is based on the map seed and deterministically written by the devs to always give the same result on all clients.

viewtopic.php?f=25&t=12451#p83708
Rseding91 wrote:Factorio doesn't allow you to read system time and the math.random uses the determistic Factorio random based off the save game. That means you can use math.random all you want and it won't break the game or cause desyncs.
@TheSAguy: The code looks fine to me, did you have any problems with it?
Keep in mind that math.random(100) gives random numbers from 1 to 100 (it excludes 0) therefore the if statement can only execute once the evolution_factor is >= 0.01.

Re: Drawing a blank - math.random

Posted: Thu Jul 28, 2016 8:04 pm
by TheSAguy
daniel34 wrote:
mojo2012 wrote:Hi, somewhere I read that math.random and multiplayer domt go too well together ... just as a sidenote
Using math.random is perfectly fine, even in multiplayer. It is based on the map seed and deterministically written by the devs to always give the same result on all clients.

viewtopic.php?f=25&t=12451#p83708
Rseding91 wrote:Factorio doesn't allow you to read system time and the math.random uses the determistic Factorio random based off the save game. That means you can use math.random all you want and it won't break the game or cause desyncs.
@TheSAguy: The code looks fine to me, did you have any problems with it?
Keep in mind that math.random(100) gives random numbers from 1 to 100 (it excludes 0) therefore the if statement can only execute once the evolution_factor is >= 0.01.
Thanks for the response Daniel.
The code works, I just wanted to check my logic/sanity.