Ive heard about math random, like math.random(1, 3) but every chance is like 50%, 50%. For example:
local chance = {
["Basic"] = 1,
["Legendary"] = 2
}
math.random(1, 2) -- 1 is 50%, 2 is 50%
-- or
local chance = {
["Basic"] = 1,
["Legendary"] = 2,
["Special"] = 3
}
math.random(1, 3) -- All 3 are the same chances
1 and 2 and 3 are equalvient to 25%.
But what about 75% for 1?
1% for 2?
0.5% for 3?
However, doing math.random(1, 100) and then checking between with > < would
be very silly and stressful.
local Chances = {
Basic = 750, -- 75 %
Rare = 500, -- 50 %
Epic = 250, -- 25 %
Legendary = 10, -- 1 %
Special = 5 -- 0.5%
}
local function RandomFromWeightedTable(OrderedTable)
local TotalWeight = 0
for Piece, Weight in pairs(OrderedTable) do
TotalWeight += Weight
end
local Chance = Random.new():NextInteger(1, TotalWeight)
local Counter = 0
for Piece, Weight in pairs(OrderedTable) do
Counter += Weight
if Chance <= Counter then
return Piece
end
end
end
print(RandomFromWeightedTable(Chances))
Yeah, but if its not 1 i want to always have basic, and percentages.
its like if not chance 1 and if chance 1 and 2 and 3 and 4 and 5 ... and not 1. thats very stressfull,