0

I am creating a raffle and at the moment have a database with username and the amount of tickets assigned to them. The only fair way I can think of selecting a winner fairly i.e. the more tickets you have, the higher the odds of winning are; is by creating a separate table with users names listed multiplied by the amount of tickets they have and randomly selecting one. This would create an enormous database. What is the best way to go about selecting the winner? Preferably via PHP.

EDIT* Users gain ticket i.e. +1 to 'tickets'. The tickets do not actually have anything assigned to them to make them different from the rest.

Username Tickets Tinman 55

Thank you in advance.

  • hmmm, do you mean create another table rather than database just to do the selection? –  Mar 31 '14 at 14:04
  • all you need is a random number generator; it will give a number and you need a transformation to map that number to one of your username-ticket combinations. –  Mar 31 '14 at 14:05
  • Is your raffle being audited and if so, how detailed is the auditing and what sort of evidence do you need to keep to show that it was conducted fairly? – Nacht Blaad Mar 31 '14 at 14:24

2 Answers2

0

You would just have a table of users and raffle ticket numbers...

John  108
John  109
John  110
Paul  111

John has 3 times as many chances of winning as Paul.

Strawberry
  • 33,750
  • 13
  • 40
  • 57
0

You can use this function:

/**
* getRandomWeightedElement()
* Utility function for getting random values with weighting.
* Pass in an associative array, such as array('A'=>5, 'B'=>45, 'C'=>50)
* An array like this means that "A" has a 5% chance of being selected, "B" 45%, and "C" 50%.
* The return value is the array key, A, B, or C in this case.  Note that the values assigned
* do not have to be percentages.  The values are simply relative to each other.  If one value
* weight was 2, and the other weight of 1, the value with the weight of 2 has about a 66%
* chance of being selected.  Also note that weights should be integers.
* 
* @param array $weightedValues
*/
function getRandomWeightedElement(array $weightedValues) {
  $rand = mt_rand(1, (int) array_sum($weightedValues));

  foreach ($weightedValues as $key => $value) {
    $rand -= $value;
    if ($rand <= 0) {
      return $key;
    }
  }
}

Source: https://stackoverflow.com/a/11872928/3449528

Community
  • 1
  • 1
Emilio
  • 326
  • 1
  • 9