Currently there is only one random generator that can't be seeded locally.
It would be nice (for RSO especially) if we could create our own random generator in lua and seed it. Currently RSO has one thats implemented in lua - using readily available one wouldmake thing easier (and faster).
Creation of separate random generators
Re: Creation of separate random generators
See LuaGameScript.create_random_generator.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
Re: Creation of separate random generators
It would be nice if LuaGameScript.create_random_generator would take more seed bits.
I would want at least 3 ints because I need to create random numbers for map generation that are dependent on the map seed and location of the chunk like this:
local chunk_rng = LuaGameScript.create_random_generator(map_seed, chunk_x, chunk_y)
If one needs more than one rng per chunk then 4 ints would be nice:
local chunk_tree_rng = LuaGameScript.create_random_generator(map_seed, chunk_x, chunk_y, 23)
The most flexible would be to support a variable number of args or an array/table of values as seed.
I would want at least 3 ints because I need to create random numbers for map generation that are dependent on the map seed and location of the chunk like this:
local chunk_rng = LuaGameScript.create_random_generator(map_seed, chunk_x, chunk_y)
If one needs more than one rng per chunk then 4 ints would be nice:
local chunk_tree_rng = LuaGameScript.create_random_generator(map_seed, chunk_x, chunk_y, 23)
The most flexible would be to support a variable number of args or an array/table of values as seed.
Re: Creation of separate random generators
https://lua-api.factorio.com/latest/Lua ... _generator
Yes, it would be nice to have the random based on list of values:
LuaGameScript.create_random_generator({a, b, c, d, e, f})
Yes, it would be nice to have the random based on list of values:
LuaGameScript.create_random_generator({a, b, c, d, e, f})
Re: Creation of separate random generators
You can always combine the ints yourself, similar to standard methods of computing hashcodes. From a quick google, something like:
Although since lua numbers are doubles, you may need to modulo it to prevent it going above the uint32 limit of 4,294,967,295.
Code: Select all
local seed = 7
seed = seed * 31 + map_seed
seed = seed * 31 + chunkX
seed = seed * 31 + chunkY
Re: Creation of separate random generators
Then the map will look the same going one tile south or 31 tiles east.Boodals wrote: Fri Oct 18, 2019 1:20 pm You can always combine the ints yourself, similar to standard methods of computing hashcodes. From a quick google, something like:Although since lua numbers are doubles, you may need to modulo it to prevent it going above the uint32 limit of 4,294,967,295.Code: Select all
local seed = 7 seed = seed * 31 + map_seed seed = seed * 31 + chunkX seed = seed * 31 + chunkY
Re: Creation of separate random generators
Code: Select all
local seed = 7 + math.pi -- some irrational number
local a = (seed - math.floor (seed))^0.5 * 4000000000 -- square root of irrational is also irrational
local seed_1 = math.floor (a) -- whole number of a, from 0 up to 4000000000
local b = (a - math.floor (a))^0.5 * 4000000000 -- next square root from irrational number
local seed_2 = math.floor (b) -- next seed
local value = noise_function({x, y, surface_id, seed})
Last edited by darkfrei on Sat Oct 19, 2019 11:50 am, edited 1 time in total.
Re: Creation of separate random generators
While pi is irrational math.pi is not. Seed2 will only have something like 16 bit random.darkfrei wrote: Fri Oct 18, 2019 9:30 pmBut we cannot make good noise with code something likeCode: Select all
local seed = 7 + math.pi -- some irrational number local a = (seed - math.floor (seed))^0.5 * 4000000000 -- square root of irrational is also irrational local seed_1 = math.floor (a) -- whole number of a, from 0 up to 4000000000 local b = (a - math.floor (a))^0.5 * 4000000000 -- next square root from irrational number local seed_2 = seed_1 = math.floor (b) -- next seed
local value = noise_function({x, y, surface_id, seed})