開始使用免費開始

從離散均勻分配取樣

Tom 有一顆一般的六面骰,面上標示 1 到 6。這個練習中,你會使用離散均勻分配(非常適合用來以均勻機率取樣整數值),來模擬擲 Tom 的骰子 1,000 次,然後將結果視覺化!

以下模組已為你匯入:將 seaborn 命名為 snsscipy.stats 命名為 st,以及 matplotlib.pyplot 命名為 plt

本練習屬於課程

Python 的 Monte Carlo 模擬

檢視課程

練習說明

  • 定義在下一步 .rvs() 取樣時要用的 lowhigh;你的分配應均勻地包含從 1(可能的最低點數)到 6(可能的最高點數)的整數值。
  • 從由 st.randint 表示、值為 1 到 6 的離散均勻分配中取樣 1,000 次。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Define low and high for use in rvs sampling below
low = ____
high = ____
# Sample 1,000 times from the discrete uniform distribution
samples = ____

samples_dict = {'nums':samples}
sns.histplot(x='nums', data=samples_dict, bins=6, binwidth=0.3)
plt.show()
編輯並執行程式碼