unibiased flip:找出头部多少次和尾部多少次的概率

时间:2012-09-22 04:18:20

标签: python

我正在尝试开发一种功能,该功能打印出打印的次数h给出的概率除以打印的总次数h或t。

这是我的代码 def unbiasedFlip(n,p):

for i in range(n+1):
    p=Pr(Heads)
    n=Totalflips
    if num1>=p and num2<p:
        print(Heads)
    elif num1>=(1-p) and num2<(1-p):
        print(Tails)

num1和num2是应该通过if函数生成的两个随机数。和可能性。当我运行程序时,我得到的错误是我没有定义pr或head。

2 个答案:

答案 0 :(得分:1)

注意:此代码可能不是您真正想要的,但我认为它可以在某种程度上帮助您......我希望如此......

无论如何,在查看我的可行解决方案之前,我建议你尝试学习Python(synthax,如何创建函数,创建随机数等)。你会发现它很容易学习,你会完全喜欢它! :P

您可以找到几种学习Python的方法(书籍,在线课程/文档,沉迷于Python XD的朋友等)。

请查看以下链接,例如:http://docs.python.org/tutorial/

请注意,拥有清晰易懂的代码有助于我们了解您的问题,并为您提供更好的机会来更好地回答您的问题;)。


这是一个简单的代码,我建议您专注于仔细阅读评论

import random 

# The function "prob_head" below return the number of head divided by the number of coin toss
# The input variable "number_toss" is number of times we toss a coin
def prob_head(number_toss):

    # "heads" is our number of heads. 
    # Initially it is equal to 0
    heads = 0

    # We toss a coin "number_toss" times...
    for i in range(0, number_toss):
        # We create a random number "flip" comprised in {0,1}        
        flip = int(random.random()*2)

        # Let's say we follow the following rule:
        # If "flip" = 0, then it's a head
        # Else, if "flip" = 1, then it's a tail

        if (flip == 0):
            # "flip" = 0, so it's a head !
            # We have to increment the number of "heads" by 1:
            heads=heads + 1 

    return float(heads)/number_toss

# Here's a test of our function: "prob_head"
my_number_toss = 100
my_head_probability = prob_head(my_number_toss)

print "Probability of heads = "+str(my_head_probability)

输出示例:

  

头部概率= 0.41


上面的代码让您了解模拟正常的硬币投掷。

重新阅读你的评论后,我想我更了解你真正想要的东西,所以我添加了这个额外的部分......

下面的代码代表了一种模拟“欺骗”/“虚假”硬币投掷游戏的方法

注意我的评论......

# The function "unbiasedFlip" returns the average probability of heads considering "n" coin 
# The variable "p" is a fixed probability condition for getting a head.
def unbiasedFlip(n, p):

    # The number of heads, initially set to 0
    heads = 0

    # We toss a coin n times...
    for i in range(0, n):

        # We generate "prob_heads": a random float number such that "prob_heads" < 1
        prob_heads = float(random.random())

        # If "prob_heads" is greater of equal to "p", then we have a head 
        # and we increase the number of heads "heads" by 1:
        if prob_heads>=p:
            heads = heads+1

    # We return the average probability of heads, considering n coin tosses
    # Note: we don't need to return the average prob. for Tails since:
    # it's equal to 1-Avg_Prob(Heads)              
    return float(heads)/n

# An example for testing our function...
# We consider 100 coin toss
my_number_toss = 100

# We want a Head only if our generated probability of head is greater or equal to 0.8
# In fact, considering that the random number generator generates equally probability numbers
# (which means that it provides as many chance to give a Tail or a Head)
# it would be like saying: "we want a probability of 1-0.8 =0.2 chance of getting a head"
my_defined_prob_heads = 0.8

# We get our average probability of heads...
average_prob_heads = unbiasedFlip(my_number_toss, my_defined_prob_heads)
# We get our average probability of tails = 1-Avg_Prob(Heads)
average_prob_tails = 1-average_prob_heads

# We print the results...
print "- Number of toss = "+str(my_number_toss)
print "- Defined probability for head = "+str(my_defined_prob_heads)
print "- Average P(Heads) for n tosses = "+str(average_prob_heads)
print "- Average P(Tails) for n tosses = "+str(average_prob_tails)

输出示例:

- Number of toss = 100
- Defined probability for head = 0.8
- Average P(Heads) for n tosses = 0.24
- Average P(Tails) for n tosses = 0.76

希望这有助于交配。

如果您有疑问或者某些事情不明确,请告诉我。

答案 1 :(得分:0)

首先,我们生成一个随机的coinflip序列:

import random
n = 100 # number of flips
p = 0.5 # P(Heads) - 0.5 is a fair coin
flips = ['H' if (random.random() < p) else 'T' for flipnr in xrange(n)]
print flips

接下来,我们计算头尾数:

nheads = flips.count('H')
ntails = flips.count('T')

并计算机会:

phead = float(nheads) / (nheads + ntails)

注意(在Python 2中)我们需要通过将其中一个变量强制转换为float来强制进行浮点除法(这在Python 3中已得到修复)。