Python

Python: NumPy 使用亂數種子來重現亂數結果

random.seed() 使用方式

import numpy as np

for _ in range(5):
    np.random.seed(10)
    a = np.random.random((10,))
    print(a)

Output

[0.77132064 0.02075195 0.63364823 0.74880388 0.49850701 0.22479665
 0.19806286 0.76053071 0.16911084 0.08833981]
[0.77132064 0.02075195 0.63364823 0.74880388 0.49850701 0.22479665
 0.19806286 0.76053071 0.16911084 0.08833981]
[0.77132064 0.02075195 0.63364823 0.74880388 0.49850701 0.22479665
 0.19806286 0.76053071 0.16911084 0.08833981]
[0.77132064 0.02075195 0.63364823 0.74880388 0.49850701 0.22479665
 0.19806286 0.76053071 0.16911084 0.08833981]
[0.77132064 0.02075195 0.63364823 0.74880388 0.49850701 0.22479665
 0.19806286 0.76053071 0.16911084 0.08833981]

錯誤用法

import numpy as np
np.random.seed(10)

for _ in range(5):
    a = np.random.random((10,))
    print(a)

Output

[0.77132064 0.02075195 0.63364823 0.74880388 0.49850701 0.22479665
 0.19806286 0.76053071 0.16911084 0.08833981]
[0.68535982 0.95339335 0.00394827 0.51219226 0.81262096 0.61252607
 0.72175532 0.29187607 0.91777412 0.71457578]
[0.54254437 0.14217005 0.37334076 0.67413362 0.44183317 0.43401399
 0.61776698 0.51313824 0.65039718 0.60103895]
[0.8052232  0.52164715 0.90864888 0.31923609 0.09045935 0.30070006
 0.11398436 0.82868133 0.04689632 0.62628715]
[0.54758616 0.819287   0.19894754 0.8568503  0.35165264 0.75464769
 0.29596171 0.88393648 0.32551164 0.1650159 ]

留下一個回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *