✓ Accepted answer
local rarities = {
Common = { probability = 699/1000 };
Uncommon = { probability = 200/1000 };
Rare = { probability = 100/1000 };
Legendary = { probability = 1/1000 };
};
function getRarity()
local probability = Random.new(tick()):NextNumber()
local cumulativeProbability = 0
for name, item in pairs(rarities) do
cumulativeProbability = cumulativeProbability + item.probability
if probability <= cumulativeProbability then
return name, item
end
end
end
So here, you want the probabilities to add up to 1.
IE: 699/1000 + 200/1000 + 100/1000 + 1/1000 = 1
So if you were to change the probabilities, you need to make sure that the probabilities add up to 1.
You may notice how the code is written as 100/1000 instead of 1/10 for example. This is because it's a lot easier to see the distribution of things when all items have a common denominator.
1 comment
I really like this answer, but unfortunately I'm going to choose Rodrigo's answer. Hope you understand :), I'll upvote your answer anyways