Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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.

2 answers

✓ Accepted answer

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

1 comment

0

When I used that I just got the same identical coordinates 3 times in a row each time I ran the code.

This discussion is preserved as it was. New answers, comments, and votes are closed.