[Solved] Drawing a blank - math.random

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

[Solved] Drawing a blank - math.random

Post 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?
Last edited by TheSAguy on Fri Jul 29, 2016 7:45 pm, edited 1 time in total.

User avatar
mojo2012
Inserter
Inserter
Posts: 25
Joined: Fri Apr 01, 2016 8:05 am
Contact:

Re: Drawing a blank - math.random

Post 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

TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Drawing a blank - math.random

Post by TheSAguy »

What would you recommend using besides Random.Math for MP then?

daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: Drawing a blank - math.random

Post 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.
quick links: log file | graphical issues | wiki

TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Drawing a blank - math.random

Post 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.

Post Reply

Return to “Modding help”