Imagine a jackpot where users can bet as much as they want, and each bet increases their winning chance. Given a roll [0-100]
, how would you calculate the winner?
My solution and the pitfall
To determine winning users, you could check if a drawn roll [0-100]
falls between a stack of users' winning probabilities.
Winning probability for each user is calculated by applying Fc(Bet, Pot Size) = Bet * 100 / Pot size
.
The only algorithm I have in mind to determine who the winner is, is to perform math server-side and iterate each user's winning probability, and check if roll
falls within:
user_bets = [250,250,500]
pot_size = sum(user_bets)
roll = 32.21;
cur_range = [0,0]
for bet in user_bets:
win_chance = bet * 100 / pot_size
cur_range = [ cur_range[1] , win_chance + cur_range[1] ]
if roll >= cur_range[0] and roll <= cur_range[1]:
# cur_range[0] = 25, cur_range[1] = 50
# We found the winner
break
Goal
The goal is to possibly adopt a one-liner algorithm to determine the winner within SQL
language, whereas no iteration is involved
Graphic example
User X = BET = $13.000
User Y = BET = $6.500
User Z = BET = $3.250
POT_SIZE = $22.750
----------------------------------------------------------------------------------------------------
User X Win range = 0 -> 57,14 (57% win chance)
User Y Win range = 57.15 -> 85,71 (29% win chance)
User Z Win range = 85,72 -> 100 (14% win chance)
----------------------------------------------------------------------------------------------------
ROLL Example = 32.27...
WINNER = User X
----------------------------------------------------------------------------------------------------
ROLL Example = 91.38...
WINNER = User Z