Trying to create a random pattern with math.random

Trying to create a random pattern with math.random, first post here, newish to scripting.

Example:

for i = 1, 50 do
		
	local rand1 = math.random(-i, i)
	local rand2 = math.random(-i, i)

	part1.CFrame = CFrame.new(rand1, rand2), 0, -i)

end

This won’t work as the [i] will keep increasing, so the numbers will be in-between 1 and 50, not exactly -i or i and i or -1

What can I use for rand1 and rand2 to be exactly equal to -i or i?

1 Like

So, you want the random to be either a positive or negative?

local rand = (math.random() == 0 and -1 or 1) * i
1 Like

Yes, I want the random to be either positive or negative. So on the first loop it would be:
-1 or 1,
second loop:
-2 or 2
and so on…
-3 or 3

Do you mean something like this? rand1 and rand2 are randomly positive i or negative i.

for i = 1, 50 do
		
	local rand1 = (math.random(0, 1)*2 - 1) * i
	local rand2 = (math.random(0, 1)*2 - 1) * i

	part1.CFrame = CFrame.new(rand1, rand2), 0, -i)

end

Written on mobile

3 Likes

ah yes! this should work nicely. Thank you!