[Resolved] Random Question

Place to get help with not working mods / modding interface.
Post Reply
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

[Resolved] Random Question

Post by TheSAguy »

Why is my code below creating a line vs. random.
I'd expect the x and y values to be random and thus the chart to be all over the place.

Code: Select all

			local radius = 2.5
			local x_offset = math.random(-250, 250) 
			local y_offset = math.random(-250, 250) 
			local position = {x_offset, y_offset}
			local area = {{x_offset - radius, x_offset - radius}, {x_offset + radius, x_offset + radius}}			

		
			for _,force in pairs( game.forces )do
				force.chart( game.surfaces[1], area)		
			end
Image
Last edited by TheSAguy on Tue Jun 18, 2019 7:37 pm, edited 1 time in total.

User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2633
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: Random Question

Post by steinio »

because math.random with values smaller then 317 results in the same value: viewtopic.php?t=19353#p123786
Image

Transport Belt Repair Man

View unread Posts

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: Random Question

Post by eduran »

Code: Select all

local area = {{x_offset - radius, x_offset - radius}, {x_offset + radius, x_offset + radius}}
You are using x_offset twice and y_offset not at all.
steinio wrote:
Tue Jun 18, 2019 7:17 pm
because math.random with values smaller then 317 results in the same value: viewtopic.php?t=19353#p123786
That is not true. It produces deterministic pseudo-random numbers.

TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Random Question

Post by TheSAguy »

eduran wrote:
Tue Jun 18, 2019 7:24 pm

Code: Select all

local area = {{x_offset - radius, x_offset - radius}, {x_offset + radius, x_offset + radius}}
You are using x_offset twice and y_offset not at all.

face-palm!

Thanks,.

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

Re: Random Question

Post by eradicator »

The whole "seeds below x produce same random output" is a problem with LuaRandomGenerator, not math.random anyway. And even then only if you run *multiple generators in parrallel*. Totally not related to this.

You can't chart less than one chunk, so your very small radius will have no effect unless your position is really close to a chunk border.

Code: Select all

/c
for i=1,10 do
  local r = 0.0001
  local x,y = math.random(-250,250), math.random(-250,250)
  local area = {{x-r,y-r},{x+r,y+r}}
  game.forces.player.chart(game.surfaces.nauvis,area)
  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.

Post Reply

Return to “Modding help”