Python中的相依概率

2024-04-25 01:43:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用Python为自己制作一些东西,在我的例子中,这将决定在一副牌中绘制某张牌的可能性。牌组的大小由一个原始输入决定,也就是你试图计算出抽牌概率的牌的数量。现在,我有:

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张牌,并在“draw”时减少x的数量,并为每个“draw”更改y。而且它必须工作很多次,这取决于用户想要什么。谢谢!(希望这有意义…)


Tags: ininputrawifitaremanyhow
1条回答
网友
1楼 · 发布于 2024-04-25 01:43:47

假设你有n张牌,你画了m张。可能的组合是n!/(n-m)!如果你在寻找绘制y张卡片(y<;=m)的概率,它占n张卡片总数的x。您必须考虑两个数量: 1从x张牌中选出y张牌的可能方法就是x!/(x-y)!然后可能的方法是把这个有序的集合放入m个插槽,或者把其他m-y卡放到m个卡的集合中,也就是m!/(m-y)! 这两个量的乘积表示有利事件,而第一个数字表示事件总数。所以你的可能性是

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

我希望我没有错过任何东西:)

相关问题 更多 >