概率练习
在这个练习中,你将模拟掷硬币和掷骰子,计算下列结果的比例。
- 两次均衡掷硬币得到两次正面
- 三次均衡掷硬币得到一次正面
- P(H) = 0.6 时三次非均衡掷硬币得到一次正面
- 一次掷骰子得到偶数
- 两次掷骰子得到相同值
然后,你在下面的练习中对比这些比例的概率。
模拟掷硬币时,0 代表正面,1 代表反面。模拟掷骰子时,使用正确的整数,匹配标准六面骰子上的数字。
Coin Flips and Die Rolls¶
Use NumPy to create simulations and compute proportions for the following outcomes. The first one is done for you.
port numpy as np
# import numpy
import numpy as np
1. Two fair coin flips produce exactly two heads
# simulate 1 million tests of two fair coin flips
tests = np.random.randint(2, size=(int(1e6), 2))
# sums of all tests
# simulate 1 million tests of two fair coin flips
tests = np.random.randint(2, size=(int(1e6), 2))
# sums of all tests
test_sums = tests.sum(axis=1)
# proportion of tests that produced exactly two heads
(test_sums == 0).mean(