Message to Devforum mods, this is the most relevant topic i could find to this post, so please do not flag it.
I need help,
Im making an money system, and i need help with calculating.
You choose how much money you want to Give and how much money the opponent wants to give. Then the chance will either choose the opponent or the client based on the amount of cash given.
For example i want it like this:
I give 950 money
Opponent gives 100 money
What would my chance in percent be out of 100%? What would the opponents chance in percent be out of 100%? The max is 100 percent.
My chance would be higher since im giving more money and the opponents should be lower.
How do i calculate this? Could someone help, im bad at maths and i tried everything so far.
But if you have the same numbers, its not 50 / 50. Like at an coinflip, if you wager more money, you get an higher chance. Or its a problem with my script, because it says 50 / 50, then it says 0% and 100%
This is how i calculate the chances (amount to wager string is a number for ex.):
local Chances = {
["ClientChance"] = math.round(AmountToWagerString / total * 100), -- 75
["OpponentChance"] = math.round(AmountOpponentWagerString / total * 100), -- 75
}
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
Your script seems ok. I tested with the same chances and it seems to work.
local AmountToWagerString = 1
local AmountOpponentWagerString = 1
local total = AmountToWagerString + AmountOpponentWagerString
local Chances = {
["ClientChance"] = math.round(AmountToWagerString / total * 100),
["OpponentChance"] = math.round(AmountOpponentWagerString / total * 100),
}
print(Chances)
local random = Random.new()
local function RandomFromWeightedTable(OrderedTable)
local TotalWeight = 0
for Piece, Weight in pairs(OrderedTable) do
TotalWeight += Weight
end
local Chance = random:NextInteger(1, TotalWeight)
local Counter = 0
for Piece, Weight in pairs(OrderedTable) do
Counter += Weight
if Chance <= Counter then
return Piece
end
end
end
for i = 1, 10 do
print(i, RandomFromWeightedTable(Chances))
end