注:このコードは、あなたが実際に探しているものではないかもしれませんが、何らかの形で役立つと思いました...そう願っています...
とにかく、私の潜在的な解決策を見る前に、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
これが仲間に役立つことを願っています。
質問がある場合、または不明な点がある場合はお知らせください。