I am developing small lottery games where the participant can win one of many gifts, each having various quantities. (The code is in PHP)

Simple scenario:

I have 5 types of gifts, 1000 in total, and am expecting 1000 participations. What algorithm can I use to ensure that these 1000 gifts are distributed somewhat evenly. The time period doesn't matter.

Uneven: 2 drones given away first, then 10 tablets, etc...

| Title     | Quantity |
| Drone     | 5        |
| Tablet    | 10       |
| Hotel     | 100      |
| Gift card | 200      |
| Coupon    | 685      |

More complex scenario:

I have the same gifts and gift quantities, but I anticipate 600 participations. I wish for the drone, tablet, and hotel nights to be all given out by the 600th participation.
So there should be 400 (gift cards + coupons) left, and better if there's the same percentage left: 116 gift cards (200/685=29%) and 284 coupons (71%).

What I tried:

For the first scenario, I tried using these probabilities, and looping through the 5 gifts sequentially:
- Each gift has its own counter
- The gift's counter increases every time the gift is considered
- The gift is given if the gift's counter == originalQuantity
- The loop starts back from the drone when a gift is given

| Title     | Quantity | Probability                   |
| Drone     | 5        | 5/1000                        |
| Tablet    | 10       | 10/(1000-5)                   |
| Hotel     | 100      | 100/(1000-5-10)               |
| Gift card | 200      | 200/(1000-5-10-100)           |
| Coupon    | 685      | 685/(1000-5-10-100-200) = 1/1 |

This solution works, and by adding a coefficient to each gift (Drone, table and hotel must have 20% more chance to come out, and gift card and coupon 30% less chance), I could also handle the more complex scenario.

My question is: are there better, fairer, simpler ways to distribute these gifts ?

Edit 1: Even distribution would mean random, and have for example, after 20% of participation, have approximately 20% of each gifts given out (1 drone, 2 tablets, etc...)

  • You should make a better definition of "evenly distributed". Do you mean that the same gift does not appear in a row? Or what Is the criteria? Must it be random or can it be deterministic?
    – slepic
    Feb 17, 2020 at 20:31
  • Not sure I understand the question. Your algorithm can hand out gifts in any order you like, for example determine winner for one drone, then one tablet ... I think you need to better define "even" and "fair".
    – Martin K
    Feb 17, 2020 at 21:38

1 Answer 1


You can just do it like the lottery: shuffle and pick. It is possible that the same kind of item is picked two or more times, just as it is possible that sequential rolls with dice yield the same number, that's how random works :-)

For the case where a number of prizes should be drawn until the 600th draw, put all of these prizes in a list together with enough of the other prizes to make 600, then shuffle that list. Shuffle the remaining 400 and concatenate the shuffled lists.

  • Ok, so you're saying to keep 2 lists (600, and 400), and pick from list #1 until it's empty, and then pick from #2 ? I don't get the concatenation. Feb 18, 2020 at 8:21
  • When the lists are shuffled, you don't need to do random picks, you just take the next item from the list when you need one. By concatenating the individually shuffled lists, the first 600 items will include your main prizes, and the next 400 the minor prizes. Feb 18, 2020 at 8:27
  • However, if what you're doing is some kind of raffle, it's better to draw prizes after tickets have been sold/given. But this isn't really a software engineering problem, you need to choose a win distribution system and just implement that. Feb 18, 2020 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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