Page 1 of 1

[0.17] A real math.random is searched

Posted: Fri Jul 19, 2019 11:40 am
by XaLpHa89
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?
.
20190719132045_1.jpg
20190719132045_1.jpg (151.14 KiB) Viewed 3409 times
20190719132213_1.jpg
20190719132213_1.jpg (151.45 KiB) Viewed 3409 times

Re: [0.17] A real math.random is searched

Posted: Fri Jul 19, 2019 11:42 am
by Bilka
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.

Re: [0.17] A real math.random is searched

Posted: Fri Jul 19, 2019 12:59 pm
by XaLpHa89
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.
.

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
20190719145449_1.jpg
20190719145449_1.jpg (90.81 KiB) Viewed 3380 times

Re: [0.17] A real math.random is searched

Posted: Fri Jul 19, 2019 1:04 pm
by Bilka
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.

Re: [0.17] A real math.random is searched

Posted: Fri Jul 19, 2019 1:05 pm
by BlueTemplar

Re: [0.17] A real math.random is searched

Posted: Fri Jul 19, 2019 1:28 pm
by eradicator
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)

Re: [0.17] A real math.random is searched

Posted: Fri Jul 19, 2019 1:57 pm
by XaLpHa89
You're right. Let player move for 5 seconds and then create the map.

Re: [0.17] A real math.random is searched

Posted: Fri Jul 19, 2019 6:47 pm
by slippycheeze
XaLpHa89 wrote:
Fri Jul 19, 2019 1:57 pm
You're right. Let player move for 5 seconds and then create the map.
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.

Re: [0.17] A real math.random is searched

Posted: Fri Jul 19, 2019 8:03 pm
by eradicator
XaLpHa89 wrote:
Fri Jul 19, 2019 1:57 pm
You're right. Let player move for 5 seconds and then create the map.
Or make them fight a group of 2~3 small biters with the pistol.

Re: [0.17] A real math.random is searched

Posted: Wed Jul 24, 2019 12:16 pm
by XaLpHa89
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

Posted: Thu Jul 25, 2019 4:13 pm
by XaLpHa89
You can now see what I do with the many math.random.

https://github.com/M3wM3w/ComfyFactorio ... nquest.lua

Re: [0.17] A real math.random is searched

Posted: Sun Aug 04, 2019 8:15 pm
by AnthonyForPOTUS
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.

Re: [0.17] A real math.random is searched

Posted: Sun Aug 04, 2019 8:56 pm
by darkfrei
AnthonyForPOTUS wrote:
Sun Aug 04, 2019 8:15 pm
local newvalue = (oldvalue * 7) % 4294967296
will instantly give you a new pseudo random value.
But the oldvalue is always the same for the same map seed.

Re: [0.17] A real math.random is searched

Posted: Mon Aug 05, 2019 9:56 am
by mrvn
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:

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

Re: [0.17] A real math.random is searched

Posted: Wed Aug 07, 2019 1:32 am
by slippycheeze
mrvn wrote:
Mon Aug 05, 2019 9:56 am
What exactly is the problem?
They don't actually want Factorio. They think they do, but the thing they want is similar, but entirely different from, Factorio itself.