首页 > 解决方案 > python中的概率计算

问题描述

我的问题是:我有 12 名球员,其中 3 名球员分别被命名为 A、B 和 C。12名选手分为2组,每组6人。我需要计算球员 A 和 B 在同一支球队中的概率,而球员 C 在另一支球队中的概率。数学不是我的强项,因为我很确定这不是一件很难计算的事情,但如果你能帮我解决这个问题,我将不胜感激。这是我到目前为止写的:

import random

playersnumb = 12
players = list(range(12))

A = random.choice([x for x in range(12)])

B = random.choice([x for x in range(12) if x != A])

C = random.choice([x for x in range(12) if (x != A) and (x != B)])

random.shuffle(players)
team1 = (players[:6])
team2 = (players[6:])

if A in team1:
    print("Player A is in team 1")
else:
    print("Player A is in team 2")
if B in team1:
    print("Player B is in team 1")
else:
    print("Player B is in team 2")
if C in team1:
    print("Player C is in team 1")
else:
    print("Player C is in team 2")

任何帮助表示赞赏。

标签: pythonprobability

解决方案


填写 1 个列表(共 6 个)的方式数 = 12!/(6!* 6!)comb(12,6)

填充六种列表的方法数(包括 A 和 B 而不是 C)= 9!/(4!* 5!)comb(9, 4)

另外,想要找到(不是 A 而不是 B 和 C)= 9!/(5!* 4!) comb(9, 5)

>>> from math import comb
>>> comb(12, 6)
924
>>> comb(9, 4) + comb(9, 5)
252
>>> 252 / 924
0.2727272727272727

推荐阅读