2012-07-23 107 views
0

我必須實現抽獎算法。問題是我希望一些參與者有更多的機會,因爲他們有更多的積分。我怎樣才能做到這一點? 我希望簡單地把它們放在抽獎中多次,但似乎並不合法。 你知道有哪些算法可以做到嗎?隨機數算法

謝謝

+0

爲他們提供更多的「門票」,就像在門口發放門票的抽獎活動一樣,但是每張門票都有相同的賠率。 (對於在門口送出的每張門票,另一個匹配的門票存根被添加到可從中選擇的一堆。) – 2012-07-23 07:14:20

回答

1

爲什麼這不是「合法的」。如果你將你的機會數量基於若干分數,則根據他的分數將該人員添加到抽獎中X次。那個人的機會增加了。

我會以這種方式解決它。

2

僞算法:

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 
+0

參與者的順序和排序在此處是否重要? – 2015-11-22 16:40:56

1

你有一個映射: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. 

該代碼僅僅是一個例子。請記住,迭代時映射並不總是保持插入順序。