3

My Goal: Each player has an option to place a bet, and for each player the winning chance of their bet would be calculated and displayed on the web page. So as an example if player 1 bets 5000 credits and the total pot is 15000 credits, player 1 has a 33% chance to win. After this calculation I would like to pick a winner from all the players that placed a bet.

Example game:

  • Total amount : 15000 credits.

  • player 1 : 3000 credits -> 20% of winning

  • player 2 : 1500 credits -> 10% of winning

  • player 3 : 7500 credits -> 50% of winning

  • player 4 : 1200 credits -> 8% of winning

  • Player 5 : 1800 credits -> 12% of winning

How should I approach the way of picking a random winner from all the players? I was thinking of generating one random number and comparing it to the win chance somehow. Any tips?

Dax
  • 767
  • 4
  • 11
  • 29
  • Possible duplicate of [generate random integers with probabilities](https://stackoverflow.com/questions/8877249/generate-random-integers-with-probabilities) – Code-Apprentice Jun 17 '18 at 11:33
  • Can it be rounded to percentage? what about betting 1 when the totalAmount is 1000000? 0% chance? – Attersson Jun 17 '18 at 11:33
  • Maybe the answers to this question will help – Code-Apprentice Jun 17 '18 at 11:33
  • 1
    Assign each player a range of numbers from [1,100]. The range they get is based on their probability of winning. For example, Player 1 would be assigned the range [1,20] while Player 2 would be assigned [21,30]. Generate a random number in the range of 1-100. The winner is the person with the range in which the number appears. – c1moore Jun 17 '18 at 11:34
  • Possible duplicate of [Generate A Weighted Random Number](https://stackoverflow.com/questions/8435183/generate-a-weighted-random-number) – pishpish Jun 17 '18 at 11:37

1 Answers1

2

Percentage does not always work due to rounding errors. For instance betting 1 when the total pot is 1000 yields 0% chance.

I recommend drawing a random number in range [0,tototalAmount)

// player 1 bets 1500, player 2 bets 2300, player 3 bets 4000
    let bets = [1500,2300,4000];
    bets.forEach((e,i)=>bets[i]=(i==0)?e:e+bets[i-1]);
    //now bets is [1500,3800,7800]
    console.log(bets);

    let maxDraw = bets[bets.length-1];   

    //Draw
    let drawnNumber = Math.floor( Math.random() * (maxDraw) );
    
    // drawnNumber is in range 0~7799
    //so if the drawn number is in ranges:
    // 0~1499      : player 1 wins. odds are 1500/7800
    // 1500~3799   : player 2 wins 2300/7800
    // 3800 7799   : player 3 wins 4000/7800
    
    console.log("drawn number =",drawnNumber);

    // remember the above are checked in order.
    let winner = 0;
    bets.forEach((e,i)=>{
        if(!winner && drawnNumber<=bets[i])
            winner = i+1;
    });
    console.log("player",winner,"wins");

This keeps rates 100% proportional.

Attersson
  • 4,755
  • 1
  • 15
  • 29
  • Done. Check the snippet. Hit Run multiple times to test it. Trust me I have worked in the gambling sector and such algorithms passed government certifications :) – Attersson Jun 17 '18 at 13:48