 # Math.random is glitchy and isn't random

So this test is very simple, with a part called “White” in replicated storage being cloned.
Someone gave me this script to randomize its position inside of a part, but it only works for bigger parts.
I have read other topics, but they’re not too similar to my problem.

Code:

``````local part = script.Parent

local PS = part.Size

while wait() do
local white = game.ReplicatedStorage.White:Clone()

white.CFrame = part.CFrame * CFrame.new(math.random(-PS.X/2,PS.X/2),math.random(-PS.Y/2,PS.Y/2),math.random(-PS.Z/2,PS.Z/2))
print(math.random(-PS.X/2,PS.X/2),math.random(-PS.Y/2,PS.Y/2),math.random(-PS.Z/2,PS.Z/2))
white.Parent = workspace
end
``````

Also look at the y coordinate, it doesn’t change for some reason

In stead of using math.random you should use random

Example:

``````local r = Random.new()
local randomNumber = r:NextInteger(-PS.Y/2,PS.Y/2)
``````
2 Likes

Uhh, this seems a bit complicated since it uses multiple locals, how can I use it so that it randoms x, y, and z?

I think I kinda figured out how to use that, but it looks the exact same as before.

``````local part = script.Parent

local PS = part.Size

while wait() do
local white = game.ReplicatedStorage.White:Clone()

local random = Random.new()
local randomNumberX = random:NextInteger(-PS.X/2,PS.X/2)
local randomNumberY = random:NextInteger(-PS.Y/2,PS.Y/2)
local randomNumberZ = random:NextInteger(-PS.Z/2,PS.Z/2)

white.CFrame = part.CFrame * CFrame.new(randomNumberX,randomNumberY,randomNumberZ)
print(randomNumberX,randomNumberY,randomNumberZ)
white.Parent = workspace
end
``````

also sorry, I’m not good with math or cFrames

You don’t need to make a randomNumber variable, here is Random.new() in your script:

``````local ran = Random.new()
local part = script.Parent

local PS = part.Size

while wait() do
local white = game.ReplicatedStorage.White:Clone()

white.CFrame = part.CFrame * CFrame.new(rand:NextInteger(-PS.X/2,PS.X/2),math.random(-PS.Y/2,PS.Y/2),math.random(-PS.Z/2,PS.Z/2))
print(math.random(-PS.X/2,PS.X/2),math.random(-PS.Y/2,PS.Y/2),math.random(-PS.Z/2,PS.Z/2))
white.Parent = workspace
end

``````

This is because math.random most the time returns an interger instead of a decimal number.

You can do math.random(x*100)/100

1 Like

This is it with a big part:

and with a small part: (which is the size I need)

1 Like

I’m not sure how to use that since it uses -ps and ps

It’s not glitchy it’s because there are no decimal.

well how do I make it a decimal? by turning the 2 into 20 or dividing by 10?

You could multiple the given `math.random(-PS.X/2,PS.X/2)` to `math.random` since this is always a 0 to 1, something like this maybe `math.random(-PS.X/2,PS.X/2) * math.random() or Random.new():NextNumber(-PS.X/2,PS.X/2)`

Well guys, thank you so much. Yall helped me finally get the solution.

Incase anyone finds this post and needs it, here it is:

``````local part = script.Parent

local PS = part.Size

while wait() do
local white = game.ReplicatedStorage.White:Clone()
local PS = part.Size

white.CFrame = part.CFrame * CFrame.new(Random.new():NextNumber(-PS.X/2,PS.X/2),Random.new():NextNumber(-PS.Y/2,PS.Y/2),Random.new():NextNumber(-PS.Z/2,PS.Z/2))

print(Random.new():NextNumber(-PS.X/2,PS.X/2),Random.new():NextNumber(-PS.Y/2,PS.Y/2),Random.new():NextNumber(-PS.Z/2,PS.Z/2))
white.Parent = workspace
end
``````