[Done] math.random() question

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

[Done] math.random() question

Post by TheSAguy »

So I noticed that "math.random(100)" is not truly random.
I get the same value each time I load the game.

Is there anything else that I should use?

Thanks.
Last edited by TheSAguy on Fri May 11, 2018 8:14 pm, edited 1 time in total.
Bilka
Factorio Staff
Factorio Staff
Posts: 3470
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: math.random() question

Post by Bilka »

Afaik you can't seed math.random(). You can seed LuaRandomGenerator, but that will still be deterministic.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
orzelek
Smart Inserter
Smart Inserter
Posts: 3928
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: math.random() question

Post by orzelek »

It needs to be this way.
It's a deterministic randomness so it will always give same sequence of random numbers.
You can create random generator in lua - you will need to seed it tho. After that it will produce different sequence of numbers but still a deterministic one.
It's needed to make sure that multiplayer stays in synch - each client and server need to roll same numbers.
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5412
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: math.random() question

Post by Klonan »

TheSAguy wrote:So I noticed that "math.random(100)" is not truly random.
I get the same value each time I load the game.

Is there anything else that I should use?

Thanks.
It depends on the map seed and the tick you call it, so using it for runtime operations should be random enough

For, say, data stage, or specific scenario (init) uses, you can try to see another generator with some values,
Seeding with the current mods in data stage,
Or the length and value of a players username in script usage etc.
Post Reply

Return to “Modding help”