How to get a random position at the circumference of a circle?

Hello! I have a circle-shaped map, and I want npcs to spawn at a random position on the circumference of the map. How would I achieve this?

You can generate a random two-dimensional polar coordinate and convert that coordinate to a three-dimentional Cartesian coordinate. Apply the Cartesian coordinate as an offset to the origin of the map:

local function randomPositionOnCircumference(radius: number, origin: Vector3): Vector3
    local angle = Random.new():NextInteger(0, 2 * math.pi)

    local x = math.cos(angle) * radius
    local z = math.sin(angle) * radius

    return origin + Vector3.new(x, 0, z)
end
2 Likes

sorry i have no idea what you mean by that

I edited my reply with a code sample

Can you please explain how this works? I’m not too good with math, and there are some code here that I have never seen before (Random.new and :NextInteger)

it works using Radians it’s a way to represent an angle but instead of degrees which is 0 to 359 it’s from 0 to 2 * pi. That’s what that first line does it gets a random number between 0 and 2 * pi.
Then to convert that to an x and y coordinate you can simply do that by taking the sin and cos of that random angle.
If you want to learn more about it you should look into trigonometry and things like the unit circle.

Technically, [0, 2π]—where 2π is also known as “tau” τ—radians is equivalent to [0, 360] degrees

The Cartesian coordinate system is the one most people are familiar with. You learn it in school, and you even learn it through Roblox Studio. This coordinate system is describe in two dimensions as an X and Y axes that intersect and expand infinitely in their respective directions. The Y axis represents up and down, while the X axis represents left and right. Up and/or right is positive, down and/or left is negative. These axes intersect in the middle at point (0, 0), where points are plotted as (x, y).

The two-dimensional polar plane has the same concept as a (0, 0), origin, except points are not plotted as (x, y). Instead, they’re plotted as (θ, r), where a point is described as a displacement from the origin at a certain angle.

image

You can see how the latter plane is more convenient for plotting points on the circumference of a circle; we need only a random angle and a radius. From here, we can use basic trigonometry to translate a polar coordinate to a Cartesian coordinate:

This is all based off the unit circle, where the radius is 1. For radii not equal to 1, we need to factor out the denominator. This is why you see me multiplying the product of the trigonometric functions by the radius

Trigonometric functions work in radians, which is more a optimal unit for mathematically expressing angles. It’s measured as the the amount of a circle’s radii being imposed on that circle’s circumference:

image

0-360 degrees is the full range of a circle’s circumference. In radians, that’s expressed as 0-2π or 0-τ (tau). I use the Random class as it has greater convenience for generating decimal numbers. From here, I take the 3D origin of the circle and offset it by however forward or backward and left or right we must go to reach that generated point on the circumference

2 Likes

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