注:此代碼可能不是你真正需要的,但我認爲它可以幫助你以某種方式......我希望如此......
反正看我的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
希望這有助於伴侶。
讓我知道你是否有問題,或者有什麼不清楚的地方。
'Pr'和'Heads'不會被神奇地定義爲你... –
你沒有定義TotalFlips或Tails。在引用它之前,您必須爲變量分配一個值。 Google Python變量來獲取基礎知識。 – Hollister