注意:此代码可能不是您真正想要的,但我认为它可以在某种程度上帮助您......我希望如此......
无论如何,在查看我的潜在解决方案之前,我建议您尝试学习 Python(语法、如何创建函数、创建随机数等)。您会发现它很容易学习并且您会非常喜欢它!: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
希望这有助于交配。
如果您有任何问题或不清楚的地方,请告诉我。