Can someone help me understand as to why this works, I can't seem to grasp it producing accurate results in all samples.
Script:
local moduleTable = require(script.ModuleScript)
for i=1, 1000 do
print(moduleTable.chooseRarity())
end
print(moduleTable.counter)
ModuleScript:
local module = {}
module.counter = {
["Legendary"] = 0,
["Epic"] = 0,
["Rare"] = 0,
["Common"] = 0
}
module.rarities = { --
["Legendary"] = 0.5,
["Epic"] = 9.5,
["Rare"] = 30,
["Common"] = 60,
}
module.chooseRarity = function()
local randomNum = (math.random(1, 1000))*0.1
print(randomNum)
local counter = 0
for rarity, weight in module.rarities do
counter += weight
print("Rarity is ", rarity)
print("Weight is ", weight)
print("Counter is currently: ", counter)
if randomNum <= counter then
print(rarity, " Has been decided")
module.counter[rarity] += 1
return rarity
end
end
end
return module