模擬一次樂透開獎
在本章最後三個練習裡,我們會把你目前學到的重點整合起來。你將執行一個完整的模擬,根據觀察到的結果做出決策,並學習如何調整模擬模型的輸入。
我們要用模擬來判斷要不要買樂透。假設你有機會買一張樂透彩券,可以角逐最高獎金 $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))