ラッフルのアルゴリズムを実装する必要があります。問題は、より多くのポイントを持っているので、一部の参加者にもっと多くのチャンスを与えてほしいということです。どうやってやるの?ラッフルに何度も入れようと思ったのですが、合法ではないようです。それを行うことができるアルゴリズムを知っていますか?
ありがとう
疑似アルゴリズム:
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
なぜそれは「合法」ではないのでしょうか。チャンスの量をポイントの数に基づいている場合、その人のポイントに基づいて、ラッフルにX回その人を追加します。その人のチャンスが増えます。
私はこのようにそれを解決します。
マッピングがあります: participant => number of chances
。多くのプログラミング言語では、次のようにマッピングまたは辞書を宣言できます。
{"player1": 2, "player2": 5, ... many more like these}
したがって、次のように繰り返すことができます。
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.
このコードは単なる例です。マッピングは反復時に常に挿入順序を維持するとは限らないことに注意してください。