Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select raffle ticket winners

Tags:

php

mysql

I've created a script where users can get points by completing tasks and then the more points they get the higher chance they have of winning the prize. It works like a raffle ticket competition.

My problem is generating winners.

I can choose a random winner with a code like this:

    function selectWinners($count){
    $select = mysql_query("SELECT first_name from `users` ORDER BY RAND() LIMIT {$count}");
    $results = mysql_fetch_assoc($select);
    echo "Winner is {$results[first_name]}";
    }

But this is just completely random and doesn't matter at all as to how many points the users have.

I believe I've got to create a temporary table that shows the users ID for every point they have, then use a similar script to select a random ID?

I'm not entire sure, just a guess, I also don't have a clue how that would be coded.

Thanks

like image 658
Daryl Avatar asked Sep 12 '25 05:09

Daryl


1 Answers

What you're asking is really more a statistics question than a programming one.

You want each user to have a chance to win proportional to the number of entries they made ("points"). In the real world, this is the equivalent of putting each user's name into a hat once for each point they get, then drawing one name at random. The single drawing names the winner.

Programmatically, you do this by choosing a random number between zero and the total number of points awarded across all users minus one (you could, if you wish, start at one. But computers use zero and that's generally easier). Then you iterate through the users, adding each one's point total to a running sum. When the running count exceeds the random number you've chosen, the user you're currently on has won.

Imagine you have three entrants, named Joe, Bob, and Alice:

Name  Points
------------
Joe   3
Bob   8
Alice 2

It doesn't matter what order the names are in, the probabilities will work anyhow.

You select a random number between zero and 12 inclusive (12=3+8+2-1). On a 0-2, Joe wins (2=3-1). On a 3-10, Bob wins (10=8+3-1). On an 11-12, Alice wins (12=3+8+2-1). Note that the number of values included in each range is equal to the number of points - thus, the odds of a given individual being selected are (individual's points / total points), the logic you desire.

The way you check the winner is as I said above - start a sum at zero, then add 3. If 3 > randomvalue, Joe wins. Then add 8 to that. If the new 11 is greater than the random value, Bob wins. Then add 2. Now the number is greater than your random value (since 13 is higher than the max you could generate).

This could, in theory, be done in a database - but it's not really an appropriate task there. This is application logic, and I'd recommend doing it in PHP (the SQL would just be a basic SELECT of all the users, their points, and perhaps the point total).

like image 109
Borealid Avatar answered Sep 14 '25 20:09

Borealid