我想从鞋子里随身携带一张卡片。我有以下代码:
s = 4
deck = {'2':4*s,'3':4*s, '4':4*s,'5':4*s, '6':4*s,'7':4*s, '8':4*s,'9':4*s, '10':4*s,'J':4*s, 'Q':4*s,'K':4*s, 'A':4*s}
def newCard(player):
card=random.choice(deck.keys())
player.append(card)
deck[card]-=1
但我不知道如何根据牌组中的牌数来实现概率。我怎么能这样做?
答案 0 :(得分:0)
感谢安德鲁库克的评论,我有一个有效的解决方案:
def weighted_random(deck):
total = sum(deck.values())
r = randint(1, total)
for c in deck.keys():
r -= deck[c]
if r <= 0:
deck[c]-=1
return c
results = weighted_random(deck)