Deterministic random number

Place to get help with not working mods / modding interface.
Post Reply
User avatar
leadraven
Filter Inserter
Filter Inserter
Posts: 354
Joined: Fri Jan 18, 2019 7:23 pm
Contact:

Deterministic random number

Post by leadraven »

Hello.
I need a random value determined by coordinates. Currently I'm using math.randomseed for each coordinates I need. Is it a correct way? Won't it break anything in other mods? Will it work properly in multiplayer?
Or, may be, I'd better to use local instance of RNG? If so, how to create it?
How can I upgrade it to be based not only on coordinates, but also on the map seed?

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Deterministic random number

Post by darkfrei »

Do you need nice noise on the map or every position can have any value?

User avatar
leadraven
Filter Inserter
Filter Inserter
Posts: 354
Joined: Fri Jan 18, 2019 7:23 pm
Contact:

Re: Deterministic random number

Post by leadraven »

darkfrei wrote:
Tue Mar 19, 2019 8:08 am
Do you need nice noise on the map or every position can have any value?
Independent value for each position.

orzelek
Smart Inserter
Smart Inserter
Posts: 3911
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Deterministic random number

Post by orzelek »

You can use LuaRandomGenerator for that but you still need to seed it.

You can find example in RSO in method rng_for_reg_pos.
It's still not perfect since you need to do some magic to create seed from coordinates to prevent bands and symetry. And lua is not exactly nice to play with when you want to overflow int's. Some internet research might net a better method of turning x, y pair into a seed.

User avatar
leadraven
Filter Inserter
Filter Inserter
Posts: 354
Joined: Fri Jan 18, 2019 7:23 pm
Contact:

Re: Deterministic random number

Post by leadraven »

orzelek wrote:
Tue Mar 19, 2019 8:50 am
It's still not perfect since you need to do some magic to create seed from coordinates to prevent bands and symetry.
math.randomseed(x)
local seedx = math.random(math.mininteger, math.maxinteger)
math.randomseed(y)
local seedy = math.random(math.mininteger, math.maxinteger)
math.randomseed(seedx+seedy)
local rnd = math.random()

It works, but is it the "correct" way?

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Deterministic random number

Post by DaveMcW »

Calling math.randomseed() more than once in a program is bad! Factorio already calls it once with the map seed, so mods should not call it at all.

Use LuaRandomGenerator instead.

Code: Select all

function position_random(pos)
  --[[ Generator only accepts even-numbered seeds from 172 to 4294967294. ]]
  local seed = (math.floor(pos.x) % 46340 * 46340 + math.floor(pos.y) % 46340 + 86) * 2
  local generator = game.create_random_generator(seed)

  --[[ Throw away the first few numbers because they are not very random. ]] 
  for i = 1, 16 do 
    generator()  
  end
  return generator()
end

--[[ Test ]]
local player = game.players[1]
for x = player.position.x - 20, player.position.x + 20 do
  for y = player.position.y - 20, player.position.y + 20 do
    local pos = {x=x, y=y}
    player.surface.create_entity{
      name = "flying-text",
      text = math.floor(position_random(pos) * 100),
      force = player.force,
      position = pos,
    }
  end
end
Last edited by DaveMcW on Wed Mar 20, 2019 3:45 pm, edited 1 time in total.

Bilka
Factorio Staff
Factorio Staff
Posts: 3132
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Deterministic random number

Post by Bilka »

DaveMcW wrote:
Tue Mar 19, 2019 2:02 pm
Calling math.randomseed() more than once in a program is bad!
In factorio it doesnt do anything so you can call it as often as you want :P
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

Post Reply

Return to “Modding help”