Don’t approach it as using percentages, that’s just a visual way to show the user. Assign each item in the case with a weight, higher weights for more common items. Add up all of the weights of all the items you want in the case. Percentages came be made by diving the weight of each item by the total weight.
Start randomly picking items within that case and subtracting it from the total, if the total goes below zero you pick that item. Rarer items are less likely to trigger the threshold to be picked.
local randomNumber = Random.new()
local weightTable = {
["Wood Sword"] = 40,
["Stone Sword"] = 35,
["Diamond Sword"] = 15,
["Plasma Sword"] = 10
}
local function caseResult(weightTable)
local selectedItem
local totalWeight = 0
for _,value in pairs(weightTable) do
totalWeight += value
end
local ticket = randomNumber:NextInteger(1,totalWeight)
for item,value in pairs(weightTable) do
ticket -= value
print(ticket)
if ticket <= 0 then
selectedItem = item
break
end
end
return selectedItem
end
This suggestion was derived from this post…