Simulating one lottery drawing
In the last three exercises of this chapter, we will be bringing together everything you've learned so far. We will run a complete simulation, take a decision based on our observed outcomes, and learn to modify inputs to the simulation model.
We will use simulations to figure out whether or not we want to buy a lottery ticket. Suppose you have the opportunity to buy a lottery ticket which gives you a shot at a grand prize of $10,000. Since there are 1000 tickets in total, your probability of winning is 1 in 1000. Each ticket costs $10. Let's use our understanding of basic simulations to first simulate one drawing of the lottery.
This exercise is part of the course
Statistical Simulation in Python
Exercise instructions
- Define
chance_of_winning
as the probability of winning the lottery.- Remember that 1 out of the total number of lottery tickets sold will win.
- Set the
probability
list to the probabilities of receiving correspondinggains
usingchance_of_winning
. - Use
np.random.choice()
to perform one simulation of this lottery drawing.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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))