Generate 3 random numbers that add up to 100

Hello! I’m trying to make a present item in a game that has random item tier percentages every time you use it.

For example, one time you use it the rarities are like this:
Common: 20%
Rare: 50%
Epic: 30%

and another time they’re like this:
Common: 75%
Rare: 20%
Epic: 5%

The only issue I’m having is insuring the random numbers always add up to 100 every time.

Here’s the code I’ve tried

function generateRarities()
	local total = 100
	

	
	local common = math.random(total)
	print(common)
	
	total -= common

	

	
	local rare = math.random(total)
	print(rare)
	
	total -= rare

	


	local epic = math.random(total)
	print(epic)

	total -= epic
	
	local sum = common + rare + epic
	
	print(sum)
	
	if common + rare + epic == 100 then
		print("total sum = 100")

	end
	
end

generateRarities()
2 Likes

I’m actually subtracting the random number it generated from the total

Wait nevermind, found a solution.

All I had to do was make the Epic rarity the remaining amount of the total percent

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.