隨機數

Python 中級

Hugo Bowne-Anderson

Data Scientist at DataCamp

Python 中級

Python 中級

Python 中級

Python 中級

Python 中級

  • 不能低於第 0 階
  • 有 0.1% 機率會跌下樓梯
  • 打賭:你會到達第 60 階
Python 中級

怎麼解?

  • 解析法
  • 模擬此過程
    • 黑客統計!
Python 中級

隨機產生器

import numpy as np
np.random.rand()      # Pseudo-random numbers
0.9535543896720104    # Mathematical formula
np.random.seed(123)    # Starting from a seed
np.random.rand()
0.6964691855978616
np.random.rand()
0.28613933495037946
Python 中級

隨機產生器

np.random.seed(123)
np.random.rand()
0.696469185597861    # 同樣的種子:得到相同的隨機數!
np.random.rand()     # 確保「可重現性」
0.28613933495037946
Python 中級

擲硬幣

game.py

import numpy as np

np.random.seed(123)
coin = np.random.randint(0,2) # 隨機產生 0 或 1
print(coin)
0
Python 中級

擲硬幣

game.py

import numpy as np
np.random.seed(123)
coin = np.random.randint(0,2)  # 隨機產生 0 或 1
print(coin)

if coin == 0: print("heads")
else: print("tails")
0
heads
Python 中級

一起來練習吧!

Python 中級

Preparing Video For Download...