Ad
Log in to vote
0

How can I prevent repeated values using math.random?

Asked by 1 year ago
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.

2 answers

Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
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.

Ad
Log in to vote
0
Answered by 1 year ago

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

0
When I used that I just got the same identical coordinates 3 times in a row each time I ran the code. ElectricZooo 55 — 1y

Answer this question