模拟一次彩票开奖
在本章最后三个练习中,您将把目前为止学到的内容串联起来。我们将运行一次完整的模拟,基于观察到的结果做出决策,并学习如何修改模拟模型的输入。
我们将用模拟来判断是否值得购买彩票。假设您有机会购买一张可争夺 $10,000 头奖的彩票。由于总共有 1000 张票,您中奖的概率是 1/1000。每张票价格为 $10。让我们先用基本的模拟方法来模拟一次彩票开奖。
本练习是课程的一部分
Python 中的统计模拟
练习说明
- 将
chance_of_winning定义为中得彩票头奖的概率。- 记住:在售出的所有彩票中,必定有 1 张会中奖。
- 使用
chance_of_winning设置probability列表,使其对应于gains的概率。 - 使用
np.random.choice()来执行一次该彩票开奖的模拟。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Pre-defined constant variables
lottery_ticket_cost, num_tickets, grand_prize = 10, 1000, 10000
# Probability of winning
chance_of_winning = 1/____
# Simulate a single drawing of the lottery
gains = [-lottery_ticket_cost, grand_prize-lottery_ticket_cost]
probability = [1-____, ____]
outcome = np.random.choice(a=gains, size=1, p=____, replace=True)
print("Outcome of one drawing of the lottery is {}".format(outcome))