掷硬币
本练习需要使用 scipy.stats 库中的 bernoulli 对象来模拟一次掷硬币的两种可能结果,1("正面")或 0("反面"),以及使用 numpy 库(已作为 np 加载)来设置随机数生成器的种子。
您将使用 bernoulli.rvs() 函数,并通过 size 参数来模拟多次掷硬币。
您会设置随机种子,以便在每个练习中都能复现实验的随机结果。
每次实验都会得到每次掷硬币的取值。您可以用 sum() 函数对这些结果求和,在掷 10 次硬币后得到正面的次数。
本练习是课程的一部分
Python 概率基础
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import the bernoulli object from scipy.stats
from ____ import ____
# Set the random seed to reproduce the results
np.____.____(42)
# Simulate one coin flip with 35% chance of getting heads
coin_flip = ____.rvs(p=____, size=____)
print(coin_flip)