# How to calculate the amount in percent with the maximum of 100

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.

G’day!

Your money / Total amount of money * 100

So:
950 + 100 = 1050

950 / 1050 * 100 = 90.5%
100 / 1050 * 100 = 9.5%

2 Likes

Thanks, this was helpful. Does it also work with numbers like: 1. 7356, 2. 234872?

Yes.

7356 + 234872 = 242228

7356 / 242228 * 100 = 3%
234872 / 242228 * 100 = 97%

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
``````

When i do it the second time, i get 100% the other 0% with the same values.

I think it is a problem somewhere else in your script. Your `RandomFromWeightedTable` function is correct.

Check if you are updating `AmountToWagerString`, `AmountOpponentWagerString`, and the Chances table correctly.

Thanks, i found the solution, i just set the AmountToWagerString and the other to nil, now it seems to be working i guess.