[0.17] A real math.random is searched
[0.17] A real math.random is searched
To create a map, I need a few random values, in a few tests, I encountered a strange behavior of the function math.random. Which only generates a random value as soon as I move. Is there another way to create random numbers?
.
.
Last edited by XaLpHa89 on Wed Jul 24, 2019 12:16 pm, edited 2 times in total.
Re: [0.17] A real math.random is searched
Moved to modding help
Another way to create random numbers is https://lua-api.factorio.com/latest/Lua ... rator.html via https://lua-api.factorio.com/latest/Lua ... _generator which is available for mods to seed however they want.
However, any random generator you can find (or create) in Factorio has the same deterministic behaviour as you observe with math.random. If you generate the same map and do the same things you get the same numbers - that's how determinism works.
Another way to create random numbers is https://lua-api.factorio.com/latest/Lua ... rator.html via https://lua-api.factorio.com/latest/Lua ... _generator which is available for mods to seed however they want.
However, any random generator you can find (or create) in Factorio has the same deterministic behaviour as you observe with math.random. If you generate the same map and do the same things you get the same numbers - that's how determinism works.
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: [0.17] A real math.random is searched
Just for information: I do not create a mod, only a scenario.
If I use the function create_random_generator, then I get the same value each time.
.
If I use the function create_random_generator, then I get the same value each time.
.
Code: Select all
local function on_tick( event )
if game.tick % 60 == 0 then
local generator = game.create_random_generator()
game.print( generator( 1, 1000 ) )
end
end
Re: [0.17] A real math.random is searched
You're creating a new generator with the same seed each time; of course that gives you the same number. Either stop making new generators every time you call the function and just use one generator or seed them with the game tick or something.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
- BlueTemplar
- Smart Inserter
- Posts: 2421
- Joined: Fri Jun 08, 2018 2:16 pm
- Contact:
- eradicator
- Smart Inserter
- Posts: 5206
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: [0.17] A real math.random is searched
If you want to make a scenario that plays differently every time even though it has the same map seed you'll have to somehow utilize player input. For example by throwing away random numbers when the player moves.
Code: Select all
/c
script.on_event(defines.events.on_player_changed_position,function()
return math.random()
end)
script.on_event(defines.events.on_tick,function(e)
if e.tick % 60 == 0 then game.print(math.random(1000)) end
end)
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: [0.17] A real math.random is searched
You're right. Let player move for 5 seconds and then create the map.
-
- Filter Inserter
- Posts: 587
- Joined: Sun Jun 09, 2019 10:40 pm
- Contact:
Re: [0.17] A real math.random is searched
Your implementation strategy for your design goal, and the multiplayer implementation in Factorio, are incompatible. I'm sorry. You will not be able to achieve the desired result with your current strategy. It is impossible by design of the game. All randomness in Factorio is strictly PRNG randomness, with the seed deterministically determined, and with no true randomness "inside" the system, only outside in, eg, player actions.
You may be able to achieve the higher level goal in another way that doesn't conflict with how the PRNGs available inside Factorio work. It may be possible to use some lazier method of triggering behaviour, so you can have more randomness, or to use a system such as the map gen noise expression support to achieve it, or something wildly random.
Could you tell us why you are trying to find a source of more random numbers? What problem do you want to solve this way? If we know that perhaps we can help you achieve your goal better.
- eradicator
- Smart Inserter
- Posts: 5206
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: [0.17] A real math.random is searched
Or make them fight a group of 2~3 small biters with the pistol.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: [0.17] A real math.random is searched
So it really does not work with the movements or the biter in the lobby. The first map creation always looks the same. When the server starts, I could warm up the first round.
Code: Select all
function initialize_surface()
local map_gen_settings = {}
map_gen_settings.seed = math.random( 1, 2097152 )
if game.surfaces[ 'random_map' ] == nil then
game.create_surface( 'random_map', map_gen_settings )
else
rendering.clear()
game.surfaces[ 'random_map' ].clear()
game.surfaces[ 'random_map' ].map_gen_settings = map_gen_settings
end
end
Re: [0.17] A real math.random is searched
You can now see what I do with the many math.random.
https://github.com/M3wM3w/ComfyFactorio ... nquest.lua
https://github.com/M3wM3w/ComfyFactorio ... nquest.lua
-
- Inserter
- Posts: 21
- Joined: Wed May 02, 2018 12:28 am
- Contact:
Re: [0.17] A real math.random is searched
local newvalue = (oldvalue * 7) % 4294967296
will instantly give you a new pseudo random value. the 7 and 4294967296 are mutually prime the radix (7) must be greater than 1 and less than the modulus (4294967296) the returned number is an integer between 0 and the modulus-1. That is the quick and dirty way, you can further rtandomze it by takign the result modulo a number smaller than 4294967296.
will instantly give you a new pseudo random value. the 7 and 4294967296 are mutually prime the radix (7) must be greater than 1 and less than the modulus (4294967296) the returned number is an integer between 0 and the modulus-1. That is the quick and dirty way, you can further rtandomze it by takign the result modulo a number smaller than 4294967296.
Re: [0.17] A real math.random is searched
But the oldvalue is always the same for the same map seed.AnthonyForPOTUS wrote: ↑Sun Aug 04, 2019 8:15 pmlocal newvalue = (oldvalue * 7) % 4294967296
will instantly give you a new pseudo random value.
Re: [0.17] A real math.random is searched
What exactly is the problem? If you want a different map then USE A DIFFERENT MAP SEED. Otherwise, with the same seed, the map SHOULD be the same.
The error in your use of game.create_random_generator() is that you create a new one from the map seed every tick. You have to create and store the generator once and then use it again and again. Each use will give you a new number.
Note: If you need random numbers to populate a chunk in the map consider using something like this:
This gives you a random number generator that depends on the map seed and location of the chunk but does not depend on the order in which chunks are generated due to how the player moves.
The error in your use of game.create_random_generator() is that you create a new one from the map seed every tick. You have to create and store the generator once and then use it again and again. Each use will give you a new number.
Note: If you need random numbers to populate a chunk in the map consider using something like this:
Code: Select all
local x_generator = game.create_random_generator(map_seed + chunk_x)
local generator = game.create_random_generator(x_generator() * 2000000000 + chunk_y)
-
- Filter Inserter
- Posts: 587
- Joined: Sun Jun 09, 2019 10:40 pm
- Contact: