[0.17] A real math.random is searched

Place to get help with not working mods / modding interface.
Post Reply
XaLpHa89
Fast Inserter
Fast Inserter
Posts: 119
Joined: Wed Jan 24, 2018 6:59 am
Contact:

[0.17] A real math.random is searched

Post 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 3335 times
20190719132213_1.jpg
20190719132213_1.jpg (151.45 KiB) Viewed 3335 times
Last edited by XaLpHa89 on Wed Jul 24, 2019 12:16 pm, edited 2 times in total.

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

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

Post 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.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

XaLpHa89
Fast Inserter
Fast Inserter
Posts: 119
Joined: Wed Jan 24, 2018 6:59 am
Contact:

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

Post 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 3306 times

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

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

Post 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.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.


User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

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

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

XaLpHa89
Fast Inserter
Fast Inserter
Posts: 119
Joined: Wed Jan 24, 2018 6:59 am
Contact:

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

Post by XaLpHa89 »

You're right. Let player move for 5 seconds and then create the map.

slippycheeze
Filter Inserter
Filter Inserter
Posts: 587
Joined: Sun Jun 09, 2019 10:40 pm
Contact:

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

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

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

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

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

XaLpHa89
Fast Inserter
Fast Inserter
Posts: 119
Joined: Wed Jan 24, 2018 6:59 am
Contact:

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

Post 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

XaLpHa89
Fast Inserter
Fast Inserter
Posts: 119
Joined: Wed Jan 24, 2018 6:59 am
Contact:

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

Post by XaLpHa89 »

You can now see what I do with the many math.random.

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

AnthonyForPOTUS
Inserter
Inserter
Posts: 21
Joined: Wed May 02, 2018 12:28 am
Contact:

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

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

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

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

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

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

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

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

slippycheeze
Filter Inserter
Filter Inserter
Posts: 587
Joined: Sun Jun 09, 2019 10:40 pm
Contact:

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

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

Post Reply

Return to “Modding help”