For a very very very long time now I have been trying to make an accurate chance system like Bubble gum sim ([🍀MEGA LUCK🍀] Bubble Gum Simulator - Roblox) with % out of 100% and just can not figure out how.
how would i make the most accurate chance system i can possibly get out of those.
I have searched everywhere and tried tons of methods myself but the chances just dont seem accurate what so ever, people getting 1/5 millions in like 5k eggs constantly
so thats how i would get the lowest chance one, lets say for the 3.07%, would i just multiply the 3.07 by a certain amount and check if chance == the 3.07 multiplied
Not necessarily because if it is already a whole number, the weighing would be unbalanced because all of a sudden all the numbers a whole. You should use a common multiplier on all the numbers to keep the proportionality.
Modified Code:
function percentToWeight(percent)
return percent * 100 --Change this if needed, I assumed 100 because you are only going to the 100ths place
end
local Items = {
{"Table", percentToWeight(0.01)},
{"Lamp", percentToWeight(0.5)},
{"Chair", percentToWeight(0.25)},
}
local TotalWeight = 0
for _,ItemData in pairs(Items) do
TotalWeight = TotalWeight + ItemData[2]
end
local function chooseRandomItem()
local Chance = math.random(1,TotalWeight)
local Counter = 0
for _,ItemData in pairs(Items) do
Counter = Counter + ItemData[2]
if Chance <= Counter then
return ItemData[1]
end
end
end
for i = 1, 20 do
print(chooseRandomItem())
end
You just have to multiply it by the right number yes, like in the example I provided, I just multiplied everything by 10 so they were all whole numbers:
.1 → 1, 99.9 → 999
If you want to use smaller decimals, such as .01, you’d need to multiply them by 100, and for .001 by 1000 and so on.
You can use functions to calculate total weights so you can have very rare chances, such as .001% and very common chances, such as 5%.
See here (you’d need to modify this code to work for yourself obviously):
local eggs = {}
eggs.Egg1 = {
Dog = 45; -- 45 % chance
Cat = 45; -- 45 % chance
Dinosaur = 10; -- 10% chance
}
eggs.Egg2 = {
Cow = 99999; -- 99.999% chance
Rainbow_Cow = 1; -- 0.001% chance
}
function GetChanceOfEgg(egg)
local chance = 0
for eggName,eggChance in pairs(egg) do
chance += eggChance
end
return chance
end
function PickRandomFromEgg(egg)
local chance = GetChanceOfEgg(egg)
local chance = math.random(0, chance)
for eggName,eggChance in pairs(egg) do
chance -= eggChance
if chance <= 0 then
return eggName
end
end
end
local pet = PickRandomFromEgg(eggs.Egg2)
print(pet)
-- Extra Code To Test Probability:
local n = 0
repeat chosenPet = PickRandomFromEgg(eggs.Egg2) n += 1 until
chosenPet == "Rainbow_Cow"
print("It took " .. n .. " tries until you got a " .. chosenPet)
local Items = {
{"Table", 2},
{"Lamp", 0.5},
{"Chair", 0.25},
}
local multiplier = 1
for _, item in pairs(Items) do --Essentially edits the multiplier so that even the smallest decimal will become whole
local function zeros(amount)
local total = "1"
for i = 1, amount do
total = total.. "0"
end
return total
end
local split = string.split(tostring(item[2]), ".")
if split[2] ~= nil then
if tonumber(zeros(string.len(split[2]))) > multiplier then
multiplier = tonumber(zeros(string.len(split[2])))
end
end
end
print(multiplier)
for _, item in pairs(Items) do
item[2] = item[2] * multiplier
end
local TotalWeight = 0
for _,ItemData in pairs(Items) do
TotalWeight = TotalWeight + ItemData[2]
end
local function chooseRandomItem()
local Chance = math.random(1,TotalWeight)
local Counter = 0
for _,ItemData in pairs(Items) do
Counter = Counter + ItemData[2]
if Chance <= Counter then
return ItemData[1]
end
end
end
for i = 1, 20 do
print(chooseRandomItem())
end