i’ve been trying to make a infinite rarities like game but can’t seem to get the “infinite” part working.
closest i’ve gotten was this:
local bNum = require(game.ReplicatedStorage.Bnum)
function module.chooseIndex(rarityTable, luck)
local newRarityArray = {}
local totalWeight = bNum.floattobnum(0)
local accumulatedWeight = bNum.floattobnum(0)
for i, rarity in ipairs(rarityTable) do
local weight = 1 / 5
weight = bNum.pow(bNum.floattobnum(weight), bNum.sub(bNum.floattobnum(i), bNum.floattobnum(1)))
local newWeight = bNum.mul(weight, bNum.strtobnum(luck))
if bNum.me(newWeight, bNum.floattobnum(1)) then
print("Skipping weight:", bNum.bnumtofloat(newWeight))
continue
end
print("Adding weight:", bNum.bnumtofloat(newWeight))
totalWeight = bNum.add(totalWeight, newWeight)
table.insert(newRarityArray, {[rarity[1]] = {newWeight}})
end
if #newRarityArray > 0 then
print("Selected from existing rarities")
local rnd = bNum.rand(bNum.floattobnum(0), totalWeight)
local selectedRarity = "Common"
local accumulatedWeight = bNum.floattobnum(0)
for index, rarity in pairs(newRarityArray) do
for a, b in pairs(rarity) do
accumulatedWeight = bNum.add(accumulatedWeight, b[1])
if bNum.leeq(rnd, accumulatedWeight) then
print("Selected rarity:", a)
selectedRarity = a
break
end
end
end
return selectedRarity
else
print("Total Weight:", bNum.bnumtofloat(totalWeight))
print("Luck:", luck)
print("Calculating number of attempts")
local numAttempts = bNum.ceil(bNum.div(bNum.log(bNum.sub(bNum.floattobnum(1), totalWeight)), bNum.log(bNum.strtobnum(luck))))
print("Number of attempts:", bNum.bnumtofloat(numAttempts))
return "rarity#" .. bNum.bnumtofloat(numAttempts)
end
end
this works fine if your luck isn’t too high and can actually select rarities from the table
but if there are no selectable rarities, it will try to calculate a new rarity. But because totalWeight will always be 0 because it skips the rarities when the weight is above 1 and log(1) will always be 0 and you can’t devide by 0, it will always return rarity#0.
I’ve tried to use chatgpt a bunch for maybe different approaches but he doesn’t seem to help either.