0

How can I prevent repeated values using math.random?

function generateMedKit(name)
local MedKits = game.ServerStorage.MedKits:FindFirstChild(name)
local Cords = {"5, 5, 5","10, 10, 10","20, 20, 20","40, 40, 40","80, 80, 80"}
local randNum = math.random(1,#Cords)
local chosenValue = Cords[randNum]
print(chosenValue)
MedKits.Position = Vector3.new(chosenValue)
MedKits.Parent = game.Workspace
end
generateMedKit("Med1")
generateMedKit("Med2")
generateMedKit("Med3")


I am making random spawns for a medkit across multiple coordinates. Everthing works, however sometimes I get repeat Coordinates. How can I prevent two identical coordinates from being generated.

0
Fifkee 2017
1 year ago

You don't. It's psuedorandom, you're bound to get the same values over and over again the smaller the range.

However, you can attempt to manipulate by setting the randomseed, using a combination of both math.random and the Random library, or just by simply creating your own random function.

put math.randomseed(tick()) in your script somewhere