2012-09-22 20 views
0

我正在嘗試開發一個函數,該函數將打印的頭的概率以h的次數打印出的次數除以h或t的總次數打印。unibiased flip:找出頭的次數和尾數的概率

這裏是我的代碼 高清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是它們應該被通過,如果函數生成的兩個隨機數。並提供可能性。當我運行程序時出現錯誤,我沒有定義公關或頭像。

+2

'Pr'和'Heads'不會被神奇地定義爲你... –

+0

你沒有定義TotalFlips或Tails。在引用它之前,您必須爲變量分配一個值。 Google Python變量來獲取基礎知識。 – Hollister

回答

1

注:此代碼可能不是你真正需要的,但我認爲它可以幫助你以某種方式......我希望如此......

反正看我的ptential前解決方案,我建議你嘗試學習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 

希望這有助於伴侶。

讓我知道你是否有問題,或者有什麼不清楚的地方。

+0

感謝它確實幫了一些。但我也必須打印尾巴的概率。最容易混淆的部分是我如何生成兩個隨機數字。因爲我必須證明第一個數字是否大於概率,第二個數字是否小於它的H的概率,並且它的第一個數字大於或等於(1-p),而第二個數字小於(1-p )那麼它的t。我對這個問題感到困惑。但是,非常感謝您的幫助 –

+0

嘿,夥計! :) Tails的概率由頭的概率給出:'P(Tails)= 1-P(Heads)'。所以,舉個例子,如果你有一個頭等於'0.41'的概率(就像上面的例子),你會有'P(Tails)= 1-0.41 = 0.59'。要生成一個隨機數,你可以使用python的'random'。例如,在上面的代碼中,我使用'int(random.random()* 2)'在{0,1}中生成了一個隨機數。哦!我想我明白你想要什麼!是不是你想要產生正面和反面的概率的2個隨機數? – Littm

+0

嘿夥計,我重新編輯了我的帖子,考慮到您的意見。你應該檢查我的文章的最後部分。希望這可以幫助。 – Littm

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中),我們需要通過將其中一個變量轉換爲來強制進行浮點除法(這在Python 3中已修復)。