How To Get A Random Position Inside A Defined Area?

Example :

First Of All, you need two things inside workspace to visualize this, A part which will move inside the defined area and a zone or the defined area

local part = workspace:FindFirstChild("MovingPart")
local Area = workspace:FindFirstChild("Area")

Now we need a function which will return the random position

local function RandomPos()
	local size = Area.Size
	local position = Area.Position
end

Why are we getting the position and the size??
Alright we need the size of course but why position, this is because the position gives us the center of the part which we really need.

Now we get the minimum X value and maximum X value and same for another axis (Y and Z)

	local minX = position.X - size.X/2
	local maxX = position.X + size.X/2

	local minY = position.Y - size.Y/2
	local maxY = position.Y + size.Y/2

	local minZ = position.Z - size.Z/2
	local maxZ = position.Z + size.Z/2

Oh my gosh what is this :sob:

Lets imagine this first think of the area as a box in 3D. The center is your start point. The edges are how far your part can roam left, right, up, down, forward, back. Every random spot is just ā€˜somewhere between center and edge’ on each axis.

Now from that lets see here in this

local minX = position.X - size.X/2
	local maxX = position.X + size.X/2

hmm, what are we doing here bro. ok so basically here in minumum X
Okay, so basically here in minX, we’re starting at the center (which is position.X) and then going left by half the size of the area. That gives us the left wall or the minimum X position.

And maxX? That’s the opposite—we go right from the center by half the size, giving us the right wall or maximum X.

Same thing applies to Y and Z. We’re just defining the range of movement for the part, based on how big the box is.

Alright that concept is done now the random position.

Okay now… How do we get a random position?

We don’t just go like math.random(minX, maxX) because that only gives integers, and we want smooth, floaty positions.

So here’s the better way:
We use math.random() which gives a number between 0 and 1.
Then we multiply that with the total range (like maxX - minX), which gives us a number between 0 and the full width.
Finally, we add that to minX to shift it back into the real area.

So like this for X:

local randomX = math.random() * (maxX - minX) + minX
then the entire function (add a return to get the values)

local function getRandomPosition()
	local size = AreaPart.Size
	local position = AreaPart.Position

	local minX = position.X - size.X/2
	local maxX = position.X + size.X/2

	local minY = position.Y - size.Y/2
	local maxY = position.Y + size.Y/2

	local minZ = position.Z - size.Z/2
	local maxZ = position.Z + size.Z/2
-- random position maker
	local randomX = math.random() * (maxX - minX) + minX
	local randomY = math.random() * (maxY - minY) + minY
	local randomZ = math.random() * (maxZ - minZ) + minZ

	return Vector3.new(randomX, randomY, randomZ)
end

now to use this function
you have to do this yourselve but just use a while loop and call this function again and again

13 Likes

made this since i will use this in lightning natural disaster, also forgot to clarify, how will u get the position from function? - make variable and then

local position = function() -- this variable becomes the position since the function returns a value

3 Likes

Please change the title. :face_with_raised_eyebrow:
If someone is just reading the titles of posts it looks like you are asking the question about how to do it.
Something like ā€œTutorial to create random movement of a part inside a defined areaā€ might be a little more clear.

8 Likes

This is a cool tutorial but the script could be refined to something like this:

local function GetRandomPosition(Part)
	local TopRightFront = Part.Position+Part.Size/2
	local BottomLeftBack = Part.Position-Part.Size/2

	local x = math.lerp(TopRightFront.X,BottomLeftBack.X,math.random())
	local y = math.lerp(TopRightFront.Y,BottomLeftBack.Y,math.random())
	local z = math.lerp(TopRightFront.Z,BottomLeftBack.Z,math.random())

	return Vector3.new(x,y,z)
end

I’m doing what you did in 19 lines in 8 lines, or 42% of the lines you used.

7 Likes

i see, haha i am still like learning, but i really did not had still learn lerp fully, but yeah the same equation. a + (a-b) * t, can you please tell me what essentials about math i should learn…

3 Likes

Algebra is your go-to for programming. You could study linear algebra as well. Trigonometry is important if you want to work with geometrical objects, like creating rendering engines or working with Roblox CFrames. Ultimately, it all depends on what field you’re going into. But I can assure you that you would never have to learn calculus. It’s useless in this field.

3 Likes

I’m sure this will suffice in many cases, but this doesn’t respect the orientation of the area.
If you instead make use of the CFrame, and work with relative offsets, you can get a random position within a part that does respect orientation.

This may look a little something like this:

local function getRandomPositionInPart(Part: BasePart): Vector3
    local cframe = Part.CFrame
    cframe *= CFrame.new(
        Part.Size.X * (math.random() - 0.5),
        Part.Size.Y * (math.random() - 0.5),
        Part.Size.Z * (math.random() - 0.5)
    )

    return cframe.Position
end