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?
Deterministic random number
Re: Deterministic random number
Do you need nice noise on the map or every position can have any value?
Re: Deterministic random number
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.
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.
Re: Deterministic random number
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?
Re: Deterministic random number
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.
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.
Re: Deterministic random number
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.