質問のコインの確率の問題をPythonで解いてみた。(2)

 質問のコインの確率の問題Pythonで解いてみました。(^_^;

コインを10枚、同時に投げ、表の出た枚数を数える、という施行を100回、繰り返したとき、表の枚数が0枚〜10枚のそれぞれになる「理論上の」回数

 1回の試行あたりのそれぞれの確率を求めて100をかければいいのかな。(^_^;
 検算のため、カウントして求める方法と反復試行の確率の公式から求める方法の2通りの方法で求めて比較してみました。(^_^;

P.S.
 質問がキャンセルされてしまったようです。(^_^;

● ProbOfCoin2.py

# coding: UTF-8
# ProbOfCoin2.py

from time import time
import itertools
import math

def nCr(n,r):
    f = math.factorial
    return f(n) // f(r) // f(n-r)

def main():
    tm = time()  # Timer Start
    C = 'AB'    # コインの表(A)裏(B)
    total = 0
    count = [0]*11
    for c in itertools.product(C,repeat=10):
        total+=1
        count[c.count('A')]+=1
##        print(c)
    li1 = [100.0*n/total for n in count]
    print(li1)
##    print(sum(li1))
    # 検算のため、反復試行の確率の公式でも求めてみる
    li2 = [100.0*nCr(10,n)*(1.0/2)**10 for n in range(10+1)]
    print(li2)
    print(li1==li2)
    print("Runtime : %.3f [sec]"%(time()-tm))   # Timer Stop & Disp

if __name__ == '__main__':
    main()

●実行結果

[0.09765625, 0.9765625, 4.39453125, 11.71875, 20.5078125, 24.609375, 20.5078125, 11.71875, 4.39453125, 0.9765625, 0.09765625]
[0.09765625, 0.9765625, 4.39453125, 11.71875, 20.5078125, 24.609375, 20.5078125, 11.71875, 4.39453125, 0.9765625, 0.09765625]
True
Runtime : 0.003 [sec]

※参考URL
https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q10150196165
https://stackoverflow.com/questions/4941753/is-there-a-math-ncr-function-in-python
https://mathtrain.jp/hanpukushikou