Python中的依赖概率

时间:2016-03-03 22:04:38

标签: python probability

我正在尝试用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张Swamp牌,还有7张牌。问题是,如果我想画一手牌(7),我不能只计算抽出60,然后是59,然后是58,然后是53的概率。那是因为如果他们是画沼泽,概率下降,从60下降到53不会。我必须做什么,以便随机选择y卡(甲板大小)中的x卡,使用z绘制量以及在“绘制”时减少x的数量,并为每个“绘制”更改y。它必须工作多次,具体取决于用户的需求。谢谢! (希望这是有道理的......)

1 个答案:

答案 0 :(得分:0)

让我们说你有n张牌,你正在吸引他们。可能的组合是n!/(n-m)!如果你正在寻找绘制y卡的概率(y <= m),这种卡组成总n张卡的x。你必须考虑2个数量: 1.从x!/(x-y)中挑出x卡的可能方法!然后将这个有序集合的可能方法放入m个插槽,或者将其他m-y卡放入m卡片组中的哪个是m!/(m-y)! 这两个数量的产品为您提供有利的活动,而第一个数字为您提供总活动。所以你的概率是

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

我希望我没有错过任何事情:)