﻿import random  # 乱数を扱うためのモジュールをインポート
n = 100000  # 試行回数
b1_denominator = 0
b1_numerator = 0
b2_denominator = 0
b2_numerator = 0
b3_denominator = 0
b3_numerator = 0
random.seed(1)  # 乱数のシード値を固定
for i in range(n):
    x = random.randint(0, 1)  # 上の子のデータ (0は男，1は女)
    y = random.randint(0, 1)  # 下の子のデータ (0は男，1は女)    
    if x == 0:  # (1) 上の子が男
        b1_denominator += 1
        if y == 0:  # 下の子も男
            b1_numerator += 1
    if x * y == 0:  # (2) 少なくとも1人が男
        b2_denominator += 1
        if x + y == 0:  # どちらも男
            b2_numerator += 1
    z = random.randint(0,1)  # (3) 0なら上の子，1なら下の子
    # 無作為に選んだ1人が男
    if (z == 0 and x == 0) or (z == 1 and y == 0):
        b3_denominator += 1
        if z == 0 and y == 0:  # もう1人の子も男
            b3_numerator += 1
        if z == 1 and x == 0:  # もう1人の子も男
            b3_numerator += 1

# (1) モンテカルロ法で求めた確率を表示 (※参考：理論値は1/2)
print(b1_numerator / b1_denominator)
# (2) モンテカルロ法で求めた確率を表示 (※参考：理論値は1/3)
print(b2_numerator / b2_denominator)
# (3) モンテカルロ法で求めた確率を表示 (※参考：理論値は1/2)
print(b3_numerator / b3_denominator)