Random penalty noise expression with seed gives non-random results

Post your bugs and problems so we can fix them.
Post Reply
Natha
Fast Inserter
Fast Inserter
Posts: 199
Joined: Sun Mar 15, 2015 1:48 pm
Contact:

Random penalty noise expression with seed gives non-random results

Post by Natha »

I implemented this noise expression:

Code: Select all

{
	type = "noise-expression",
	name = "random-value-0-1",
	expression = noise.to_noise_expression(noise.to_noise_expression{
	  type = "function-application",
	  function_name = "random-penalty",
	  arguments = {
		x = noise.var("x"),
		y = noise.var("y"),
		source = noise.to_noise_expression(100),
		amplitude = noise.to_noise_expression(100),
		seed = noise.var("map_seed")
	  }
	} / 100) -- cannot use noise.random_between() here because we need to consider map seed
  },
which gives this result:
Unbenannt.PNG
Unbenannt.PNG (127.45 KiB) Viewed 545 times
As you can see, values are not random for the same x-value. When I use a constant number seed, the results are random.

Genhis
Factorio Staff
Factorio Staff
Posts: 132
Joined: Wed Dec 24, 2014 8:19 am
Contact:

Re: Random penalty noise expression with seed gives non-random results

Post by Genhis »

Thanks for the report. How do you get the visualisation and what are X/Y coordinates of the tiles? Ideally I'm looking for something which I can just copy-paste and get the same result. Based on the C++ code, this is very unlikely to happen, so either you hit an edge case or your debug visualisation is incorrect.

Natha
Fast Inserter
Fast Inserter
Posts: 199
Joined: Sun Mar 15, 2015 1:48 pm
Contact:

Re: Random penalty noise expression with seed gives non-random results

Post by Natha »

Genhis wrote:
Mon Jul 08, 2024 8:10 am
Thanks for the report. How do you get the visualisation and what are X/Y coordinates of the tiles? Ideally I'm looking for something which I can just copy-paste and get the same result. Based on the C++ code, this is very unlikely to happen, so either you hit an edge case or your debug visualisation is incorrect.
I assigned this expression to a property in map_gen_settings:

Code: Select all

property_expression_names = {["probability"] = "random-value-0-1"}
(Another issue that I noticed is that if you name the property the same as the expression, it won't be taken)

Then I had a test mod which does the following upon running a command:

Code: Select all

local pos_arr = get_area_positions(get_area(player.position, 32)) -- just returns an array full of positions, 2 chunks around the player
		local props = {"probability"}
		for _,p in ipairs(pos_arr) do
			local calcresult = player.surface.calculate_tile_properties(props, {p})
			if calcresult[props[1]] then
				rendering.draw_text{text=string.format("%.4f", calcresult[props[1]][1]), color={1, 1, 1}, surface=player.surface, target=p, scale=0.5, time_to_live=600}
			end
		end
What I just figured out: the issue only occurs when I pass only one position to calculate_tile_properties. If I pass the whole array, the result is completely random (but then different result if the array changes).

Passing the map seed as literal number for random penalty seed gives the same columned result. But passing 1 as literal number gives a random result. Could this be a data type length issue?

Anyway, in my opinion the random penalty should not rely on the position array.
I'm going to setup a test mod for you to reproduce.

Natha
Fast Inserter
Fast Inserter
Posts: 199
Joined: Sun Mar 15, 2015 1:48 pm
Contact:

Re: Random penalty noise expression with seed gives non-random results

Post by Natha »

This mod shows what I mean:
/test [a, b] [p1, p2, p3]

a is calculate_tile_properties once for each position
b is calculate_tile_properties with whole position array

p1 is a random value noise expression with seed 100
p2 is with map seed variable
p3 is with a literal number which represents a map seed
Attachments
ne_0.0.1.zip
(1.26 KiB) Downloaded 13 times

Genhis
Factorio Staff
Factorio Staff
Posts: 132
Joined: Wed Dec 24, 2014 8:19 am
Contact:

Re: Random penalty noise expression with seed gives non-random results

Post by Genhis »

Natha wrote:
Mon Jul 08, 2024 6:41 pm
Passing the map seed as literal number for random penalty seed gives the same columned result. But passing 1 as literal number gives a random result. Could this be a data type length issue?
It's possible. Random penalty expects batch inputs (not single values) because it has a random generator with side effects - the first position determines the seed for the whole batch. It usually runs on a chunk.

Code: Select all

RandomGenerator rand((int32_t(x[0]) + 213) * 7919 + (int32_t(y[0] + seed) + 315) * 7907);
for (size_t i = cache.vectorSize; i-- > 0;)
  dest[i] = source[i] <= 0 ? source[i] : source[i] - rand.uniformDouble() * amplitude;
If you want to replicate base game map generation, this topic may be useful to you: viewtopic.php?f=25&t=113146
To learn more about our random generator, see this: viewtopic.php?f=34&t=70588

Post Reply

Return to “Bug Reports”