Page 1 of 1
Creation of separate random generators
Posted: Thu May 26, 2016 4:52 pm
by orzelek
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).
Re: Creation of separate random generators
Posted: Mon Oct 07, 2019 8:42 am
by Bilka
See LuaGameScript.create_random_generator.
Re: Creation of separate random generators
Posted: Mon Oct 07, 2019 9:39 am
by mrvn
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.
Re: Creation of separate random generators
Posted: Mon Oct 07, 2019 2:35 pm
by darkfrei
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})
Re: Creation of separate random generators
Posted: Fri Oct 18, 2019 1:20 pm
by Boodals
You can always combine the ints yourself, similar to standard methods of computing hashcodes. From a quick google, something like:
Code: Select all
local seed = 7
seed = seed * 31 + map_seed
seed = seed * 31 + chunkX
seed = seed * 31 + chunkY
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.
Re: Creation of separate random generators
Posted: Fri Oct 18, 2019 6:06 pm
by mrvn
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:
Code: Select all
local seed = 7
seed = seed * 31 + map_seed
seed = seed * 31 + chunkX
seed = seed * 31 + chunkY
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.
Then the map will look the same going one tile south or 31 tiles east.
Re: Creation of separate random generators
Posted: Fri Oct 18, 2019 9:30 pm
by darkfrei
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
But we cannot make good noise with code something like
local value = noise_function({x, y, surface_id, seed})
Re: Creation of separate random generators
Posted: Sat Oct 19, 2019 11:25 am
by mrvn
darkfrei wrote: Fri Oct 18, 2019 9:30 pm
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 = seed_1 = math.floor (b) -- next seed
But we cannot make good noise with code something like
local value = noise_function({x, y, surface_id, seed})
While pi is irrational math.pi is not. Seed2 will only have something like 16 bit random.