Choosing coin spawn locations in a hollow circle, need formulae

Is there a mathematical formula for choosing random positions within the shaded area? I thought about just ignoring all the chosen locations inside the white area but it’s quite large and my current formula based on a circle would choose the white circle 60% of the time.

1 Like

Assuming this is in a 3D space, you could make a function that looks something like this:

local min_distance , max_distance = 5,10
local origin = Vector3.new(0,0,0)
function is_within_ring(position)
    local distance = (origin - position).Magnitude
 
    return distance > min_distance and distance < max_distance.
end

This would get you something like result shown on the image, however, since this takes height into account this’d be closer to a sphere and not a cylinder, if you wanted it to be a cylinder, you would just have to make the y component in the distance calculation 0, like this:

function is_within_ring(position)
    local distance = (origin - position * Vector3.new(1,0,1).Magnitude
 
    return distance > min_distance and distance < max_distance.
end

You’d choose a first random number between 0 and 2*pi as a random angle in radians which are passed to sine and cosine to generate a unit vector with a random direction, and then a random number as distance between your minimum and maximum radii

Something like this should work for generating a random point in a ring around the origin, you can then offset the last position so the ring is centred around an arbitrary point:

local random = Random.new()

local function randomPointInRing(minRadius, maxRadius)
	local angle = random:NextNumber(0, 2 * math.pi) -- 0-360 degrees
	
	local cos = math.cos(angle) -- x component of a supposed unit vector
	local sin = math.sin(angle) -- y component of a supposed unit vector
	
	local radius = random:NextNumber(minRadius, maxRadius)
	local x = radius * cos -- unit * random radius
	local y = radius * sin -- unit * random radius
	
	return Vector2.new(x, y) -- or Vector3.new(x, 0, y)
end

1 Like

So what you’re saying is I get a random point inside the outer circle and eliminate any point chosen outside the min and max values. Kind of sounds like what I’m already thinking. The issue is that if a point is chosen in the shaded area then I’m good but if I choose a point inside the white circle the function tries again, and again, and again until a new point in the shaded area is chosen. This means the function could run hundreds of iterations before it finds a true statement (correct location).

True, the other solution is better since it always provides a correct result.

Oh ty, this looks good. I will test it when I can and set solution when it works.

1 Like

I played around with the numbers and while it makes no sense (size/2.75 – to get the radius of a circular part) to me it seems to work fine so I’m happy enough.

1 Like

The radius of a circular part would be 0.5 * the part’s width (or the width / 2)

Yeah, that was not the case inside a curved bowl. Like I said I played with the numbers and got some interesting results not to mention each ring had different numbers to make it work.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.