0

I have to implement an algorithm for a raffle. The problem is that i would like that some of the participant to have more chances, because they have more points. How can i do that? I thounght to simply put them many times in the raffle, but doesn't seems legit. Do you know any algorithms that can do that?

Thanks

1
  • Give them more "tickets", just like one does at a raffle where tickets are given away at the door .. but each ticket has the same odds being chosen. (For each ticket given away at a door, another matching ticket stub is added to the pile that can be chosen from.) Commented Jul 23, 2012 at 7:14

3 Answers 3

2

Pseudo algorithm:

winnerTicket <- a random number between zero and sum ticket count - 1
currentTicket <- 0
For each participant in participants ordered by id
    If winnerTicket - currentTicket > participant.ticketCount
        currentTicket += participant.ticketCount
    Else
        return participant 
Sign up to request clarification or add additional context in comments.

1 Comment

Does the order and sorting of participants matter here?
1

Why wouldn't that be "legit". If you base your amount of chance on a number of points, you add the person for X times in the raffle based on his points. That person's chance increase.

I would solve it in this way.

Comments

1

You have a mapping: participant => number of chances. In many programming languages you can declare a mapping or dictionary like this:

{"player1": 2, "player2": 5, ... many more like these}

so you can iterate like this:

accumulatedMap = {} #an empty map
total = 0
for each pair of key:count in the mapping:
    total = total + count
    accumulatedMap[key] = total

#now, get random and calculate
element = random between 1 and total, inclusive.
for each pair of key:accumulated in the mapping:
    if element <= accumulated:
        return key
#at this point, in the worst case the last key was returned.

This code is just an example. Remember that mappings don't always keep an insertion order when iterating.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.