【发布时间】:2021-07-20 20:42:44
【问题描述】:
问题如下:
模拟掷三个公平硬币并数 X 正面的数量。
- 使用您的模拟来估计 P(X = 1) 和 EX。将估计值与根据理论计算得出的真实值进行比较。
- 修改上述内容以允许 P(heads)=3/4 的有偏硬币。
我已经能够如下模拟无偏硬币:
import random
SIMULATION_COUNT = 9999999
coin_surface_dictionary = {'H':0.5, 'T': 0.5}
def get_coin_surface():
return random.choice(['H', 'T'])
def get_three_coin_surface():
list_vector = []
list_vector.append(get_coin_surface())
list_vector.append(get_coin_surface())
list_vector.append(get_coin_surface())
return list_vector
if __name__ == "__main__":
one_head_count_int = 0
for ch in range(1, SIMULATION_COUNT):
coin_surface_vector = get_three_coin_surface()
head_count_int = coin_surface_vector.count("H")
if head_count_int == 1:
one_head_count_int = one_head_count_int + 1
# END if
# END for loop
probability = one_head_count_int / SIMULATION_COUNT
print(probability)
如何通过最小限度地修改此源代码来模拟有偏见的硬币?
【问题讨论】:
标签: python simulation probability