Python 中的相关概率

Dependent probability in Python

我正在尝试在 Python 中为我自己做一些事情,这将决定在我的情况下,在牌组中抽取某张牌的可能性。牌组的大小由 raw_input 决定,您尝试计算抓牌概率的牌数也是如此。现在,我有:

from __future__ import division

t = True
while True:
    if t == True:
        numCards = int(raw_input("How many cards in your deck? Type here: "))

        print "There are " + str(numCards) + " cards in your deck."

        whatCards = raw_input("What card are you testing for how likely it is to draw them? Type here: ")
        whatCards = whatCards.capitalize()
        print whatCards + " is a great card!"

        if whatCards.endswith("s"):
            numCards2 = int(raw_input("How many " + whatCards + " are in it? "))
            whatCards = whatCards + "s"
        elif whatCards.endswith("y"):
            numCards2 = int(raw_input("How many " + whatCards[:-1] + "ies are in it? "))
        else:
            numCards2 = int(raw_input("How many " + whatCards + "s are in it?"))

        if numCards2 > 1:
            print "Ok! There are " + str(numCards2) + " " + whatCards + "s" + "!"
        else:
            print "Alright. There is " + str(1) + " " + whatCards + "!"



        '''probability = (numCards2 / numCards) + (numCards2 / (numCards-1)) + (numCards2 / (numCards-2)) + \


(numCards2 / (numCards-3)) + (numCards2 / (numCards-4)) + (numCards2 / (numCards-5))\
                  + (numCards2 / (numCards-6))'''
    probability = numCards2 / numCards

    print "Results are approximate"
    print "Decimal: " + str(probability)

    if len(str(probability)) <= 3:
        print "Percentage: " + str(probability)[2] + str(0) + "%"
    else:
        print "Percentage: " + str(probability)[2] + str(probability)[3] + "%"

    print "Fraction: " + str(numCards2) + "/" + str(numCards)
    t = False

if t == False:
    numCards = int(raw_input("How many cards in your deck? Type here: "))

    print "There are " + str(numCards) + " cards in your deck."

    whatCards = raw_input("What card are you testing for how likely it is to draw them? Type here: ")
    whatCards = whatCards.capitalize()
    print whatCards + " is a great card!"

    if whatCards.endswith("s"):
        numCards2 = int(raw_input("How many " + whatCards + " are in it? "))
        whatCards = whatCards + "s"
    elif whatCards.endswith("y"):
        numCards2 = int(raw_input("How many " + whatCards[:-1] + "ies are in it? "))
    else:
        numCards2 = int(raw_input("How many " + whatCards + "s are in it?"))

    if numCards2 > 1:
        print "Ok! There are " + str(numCards2) + " " + whatCards + "s" + "!"
    else:
        print "Alright. There is " + str(1) + " " + whatCards + "!"



    '''probability = (numCards2 / numCards) + (numCards2 / (numCards-1)) + (numCards2 / (numCards-2)) + \
                  (numCards2 / (numCards-3)) + (numCards2 / (numCards-4)) + (numCards2 / (numCards-5))\
                  + (numCards2 / (numCards-6))'''
    probability = numCards2 / numCards

    print "Results are approximate"
    print "Decimal: " + str(probability)

    if len(str(probability)) <= 3:
        print "Percentage: " + str(probability)[2] + str(0) + "%"
    else:
        print "Percentage: " + str(probability)[2] + str(probability)[3] + "%"

    print "Fraction: " + str(numCards2) + "/" + str(numCards)
    t = True

如您所见,它目前正在解决的是只有 一个 平局的可能性。然而,我的目标是让用户可以输入他们自己的抽牌次数,比如说,他们有一个 60 张牌的牌组,里面有 15 张沼泽牌,还有 7 次抽牌。问题是,如果我想抽一手牌(7),我不能让它计算抽出 60、59、58、53 的概率。那是因为如果他们要绘制沼泽,概率下降,从 60 下降到 53 不会做。我必须做些什么才能让它从 y(牌组大小)中随机选择 x 张牌,抽牌数量为 z,并在 x 为 "drawn" 时减少 x 的数量,并为每个 "draw"。而且它必须工作任意次数,具体取决于用户的需求。谢谢! (希望这是有道理的...)

假设你有 n 张牌,你要抽 m 张牌。可能的组合是 n!/(n-m)!如果您正在寻找抽取 y 张卡片 ( y<=m ) 的概率,这种卡片构成了总共 n 张卡片中的 x。您必须考虑两个数量: 1.从x张牌中挑出y张牌的可能方式是x!/(x-y)!然后是将这个有序集合放入 m 个插槽的可能方法,或者将其他 m-y 卡片放入 m!/(m-y)! 这 2 个数量的乘积为您提供有利事件,而第一个数字为您提供总事件。所以你的概率是

P = x!m!(n-m)!/(n!(x-y)!(m-y)!)

希望我没有遗漏任何东西:)