Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I prevent repeated values using math.random?

Asked by 4 years 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
4 years 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 4 years 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 — 4y

Answer this question