[Help] Need to Evenly Distribute Parts Over a Surface

I’m working on something, where i need to spawn about 30-40 parts over a surface.
These parts need to be evenly spread out.

I tried using this function to get a random x and z coordinate, however
the parts spawned unevenly. Some were very close to each other while some were too far away from each other.

local function GetRandomXZ(Ground)
return Ground.Position.X + math.random(-Ground.Size.X/2,Ground.Size.X/2),Ground.Position.Z + math.random(-Ground.Size.Z/2,Ground.Size.Z/2)
end

Any solutions to make these 30-40 parts spawn evenly?
(by evenly, i mean that i want the parts to spawn in such a way that they are equally distributed on the surface. They are neither concentrated in one region, nor missing in the other)

You need them to be procedurally generated?

Randomly distributed, or in a grid?

You could use something like this

local length = 20

for x = 0, length do
for y = 0, length do
for z = 0, length do

end

end

end

not exactly a grid.
If you play pet simulator x, you will notice that the breakable objects like coins and chests are evenly spread out on the ground. This is what I’m trying to achieve.

suppose i have 30 parts,
how do i get 30 positions, on the surface of another part, which are evenly spread out?
@Boustifail

local length = 20
local center= CenterPart
local cf = center.CFrame
local rng = math.random

for i = 1, 30 do
local part = YourPart
local x, y, z = rng(-center.Size.X/2, center.Size.X/2), rng(0, 5), rng(-center.Size.Z/2, center.Size.Z/2)
part.CFrame = cf * CFrame.new(x, y + center.Size.Y/2, z)

end

Could you just generate them in a grid and then jiggle them around inside the grid? Or are you looking for a poisson distribution like this?

2 Likes

I am not sure I understand the problem 100%.

You can pre-fill a table with possible positions and remove a random one. Then remake the table if it is empty. That will prevent most cases of picking the same values in close succession.

Check out “Poisson-disc distribution” if you want to ensure the positions are not on a grid.

In general, if you want something evenly spread out, you cannot use random. Random numbers will sometimes cluster, that is part of the randomness.

1 Like

does not solve the problem of the parts being unevenly distributed.
the parts are concentrated in one region, and the other region barely has any parts.

yes, the picture depicts how i want the parts to spawn.
the picture is exactly how i want the parts to spawn.

Update: Found the solution to my own problem.
I used Mitchell’s Best Candidate Algorithm

1 Like