# Calculating a jackpot winner based on probabilities

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

• I don't see a question here. – Yuval Filmus Oct 29 '20 at 11:30
• @YuvalFilmus it's actually on the second line. – Constantin Oct 29 '20 at 11:33
• Your question already contains a complete solution. – Yuval Filmus Oct 29 '20 at 12:30
• ((Missed&found that.) The catch may be that there's a catch rather than a pitfall: the solution presented uses iteration, while one without is required.) – greybeard Oct 29 '20 at 13:13
• @greybeard is right. The pitfall is the iteration, for me. – Constantin Oct 29 '20 at 13:16

For each user, sample the exponential distribution with rate equal to the size of that user's bet. The user with the smallest random variate wins the pot. Generation is something like $$-\log(1 - \text{random}()) / \text{bet}$$ where $$\text{random}()$$ returns a random number in $$[0, 1)$$.