mardi 26 mai 2020

How to pick a row randomly based on a number of tickets you have

I have this table called my_users

my_id | name | raffle_tickets
1     | Bob  | 3
2     | Sam  | 59
3     | Bill | 0
4     | Jane | 10
5     | Mike | 12

As you can see Sam has 59 tickets so he has the highest chance of winning. Chance of winning:

  • Sam = 59/74
  • Bob = 3/74
  • Jane = 10/74
  • Bill = 0/74
  • Mike = 12/74

PS: 74 is the number of total tickets in the table (just so you know I didn't randomly pick 74)

Based on this, how can I randomly pick a winner, but ensure those who have more raffles tickets have a higher chance of being randomly picked? Then the winner which is picked, has 1 ticket deducted from their total tickets

UPDATE my_users
SET raffle_tickets = raffle_tickets - 1
WHERE my_id = --- Then I get stuck here...



Aucun commentaire:

Enregistrer un commentaire