1
$\begingroup$

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
$\endgroup$
5
  • $\begingroup$ I don't see a question here. $\endgroup$ – Yuval Filmus Oct 29 '20 at 11:30
  • $\begingroup$ @YuvalFilmus it's actually on the second line. $\endgroup$ – Constantin Oct 29 '20 at 11:33
  • $\begingroup$ Your question already contains a complete solution. $\endgroup$ – Yuval Filmus Oct 29 '20 at 12:30
  • 1
    $\begingroup$ ((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.) $\endgroup$ – greybeard Oct 29 '20 at 13:13
  • $\begingroup$ @greybeard is right. The pitfall is the iteration, for me. $\endgroup$ – Constantin Oct 29 '20 at 13:16
0
$\begingroup$

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)$.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.