開始使用免費開始

丟硬幣

這個練習需要使用 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)
編輯並執行程式碼