Creation of separate random generators

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

Creation of separate random generators

Post 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).

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

Re: Creation of separate random generators

Post by Bilka »

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.

mrvn
Smart Inserter
Smart Inserter
Posts: 5704
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Creation of separate random generators

Post 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.

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

Re: Creation of separate random generators

Post 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})

Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

Re: Creation of separate random generators

Post 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.

mrvn
Smart Inserter
Smart Inserter
Posts: 5704
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Creation of separate random generators

Post 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.

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

Re: Creation of separate random generators

Post 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})
Last edited by darkfrei on Sat Oct 19, 2019 11:50 am, edited 1 time in total.

mrvn
Smart Inserter
Smart Inserter
Posts: 5704
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Creation of separate random generators

Post 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.

Post Reply

Return to “Implemented mod requests”