Python隨機數(shù)種子(random seed)的使用
在科學技術和機器學習等其他算法相關任務中,我們經(jīng)常需要用到隨機數(shù),為了把握隨機數(shù)的生成特性,從隨機數(shù)的隨機無序中獲得確定和秩序。我們可以利用隨機數(shù)種子(random seed)來實現(xiàn)這一目標,隨機數(shù)種子,可以使得引入了隨機數(shù)的整個程序,在多次運行中得到確定的,一致的結果。
很多博文談到隨機數(shù)種子,只是簡單論及,利用隨機數(shù)種子,可以每次生成相同的隨機數(shù)。想真正用好掌握它,對此很容易產(chǎn)生疑惑,生成相同的隨機數(shù)數(shù)怎么個相同法?隨機數(shù)種子又作何用處?
1. 隨機數(shù)種子
下面我們從實例中揭開隨機數(shù)種子的神秘面紗:
import random
# print(help(random))
def test_random_seed_in_std_lib(seed=0, cnt=3):
random.seed(seed)
print("test seed: ", seed)
for _ in range(cnt):
print(random.random())
print(random.randint(0,100))
print(random.uniform(1, 10))
print('\n')
test_random_seed_in_std_lib()
test seed: 0
0.8444218515250481
97
9.01219528753418
0.04048437818077755
65
5.373349269065314
0.9182343317851318
38
9.710199954281542
test_random_seed_in_std_lib()
test seed: 0
0.8444218515250481
97
9.01219528753418
0.04048437818077755
65
5.373349269065314
0.9182343317851318
38
9.710199954281542
test_random_seed_in_std_lib(99)
test seed: 99
0.40397807494366633
25
6.39495190686897
0.23026272839629136
17
7.8388969285727015
0.2511510083752201
49
5.777313434770537
通過兩次運行以上程序,我們得到相同的結果,這說明了以下幾點:
- 在確定了一次隨機數(shù)種子后,隨機數(shù)函數(shù),無論任何分布任何類型,在多次重復調用中(for循環(huán))生成的隨機數(shù)不同;
- 當再次聲明相同的隨機數(shù)種子時(第二次調用test_random_seed_in_std_lib函數(shù),random.seed(seed)這一行),隨機數(shù)將從“頭”開始, 按相同的順序生成隨機數(shù)。這里的“頭”,即是random.seed(seed)聲明后,隨機數(shù)函數(shù)的首次調用;
- 若指定不同的隨機數(shù)種子(seed=99),無論任何隨機數(shù)函數(shù),生成的隨機數(shù)將不同于,之前的(隨機數(shù)種子為0)的運行結果。
- 上面的幾點解釋了隨機數(shù)種子可以使得每次生成相同隨機數(shù)的具體含義。這里的相同,其實還有一種更普遍的內(nèi)涵,即環(huán)境獨立和跨平臺。上面的實驗,在任何電腦或主機,運行以上代碼,可以復現(xiàn)完全一致的結果。
以上幾點囊括了隨機數(shù)種子的基本特性,下面我們來對numpy中的隨機數(shù)種子作進一步的拓展研究。
2. numpy中的隨機數(shù)種子
import numpy as np
def test_numpy_random_seed(seed=0, cnt=3):
np.random.seed(seed)
print("test numpy seed: ", seed)
for _ in range(cnt):
print(np.random.random())
print(np.random.randn(1, 5))
print(np.random.uniform(1, 10, 5))
print('\n')
多次運行以上的test_numpy_random_seed函數(shù),你可以觀察到與使用random模塊時相似的情形,進一步驗證了我們總結的關于隨機數(shù)種子的特性。
此外,我們可以對多維隨機數(shù)組做一些有益的探索:
def test_mult_shape(seed=0):
np.random.seed(seed)
print(np.random.randn(1, 3))
print(np.random.randn(1, 2))
np.random.seed(seed)
print(np.random.randn(2, 5))
test_mult_shape()
[[1.76405235 0.40015721 0.97873798]]
[[2.2408932 1.86755799]]
[[ 1.76405235 0.40015721 0.97873798 2.2408932 1.86755799]
[-0.97727788 0.95008842 -0.15135721 -0.10321885 0.4105985 ]]
運行test_mult_shape函數(shù),我們發(fā)現(xiàn),設定相同的隨機數(shù)組,兩次運行兩個一行的多維正態(tài)分布的結果,與一次運行兩行的多維正態(tài)分布的結果的第一行完全相同。
這個結果,說明了對相同類型的隨機數(shù)分布,形狀特征不會影響分布的生成秩序,程序中,np.random.randn(1, 2),這一行不像是第二次運行多維正態(tài)分布的隨機數(shù)組,它"幾乎"是后綴于它的前一行一次性生成的。
3. 隨機數(shù)“順序”的奧秘
至此,我們對隨機數(shù)生成順序有了初步印象,但是這里的順序,其實比我們的樸素觀察更復雜,我們來進一步考察這一點。
def test_numpy_random_seed_order(seed=0):
np.random.seed(seed)
print(np.random.random())
# print(np.random.randint(1, 10))
print(np.random.randn(1, 5))
np.random.seed(seed)
print(np.random.randn(2, 5))
test_numpy_random_seed_order()
0.5488135039273248
[[ 0.74159174 1.55291372 -2.2683282 1.33354538 -0.84272405]]
[[ 1.76405235 0.40015721 0.97873798 2.2408932 1.86755799]
[-0.97727788 0.95008842 -0.15135721 -0.10321885 0.4105985 ]]
運行以上程序,我們看到,設定了相同的隨機數(shù)種子,np.random.randn(1, 5)看起來是第一次運行多維正態(tài)分布數(shù)組,實際上并不是,np.random.randn(2, 5)才是真正的第一次運行多維正態(tài)分布隨機數(shù)組。
這說明,前面的np.random.random()對np.random.randn產(chǎn)生了干擾,使得這次正態(tài)分布的隨機數(shù)組中的任何一個數(shù),都不在np.random.randn(2, 5)中,這樣它顯示了一種不可把握的隨機性。
我們可以把這一點考察得更加深入一點:
def test_numpy_random_seed_order_further(seed=0, randint_high=10):
np.random.seed(seed)
print(np.random.randint(1, randint_high))
print(np.random.randn(1, 5))
np.random.seed(seed)
print(np.random.randn(2, 5))
test_numpy_random_seed_order_further()
6
[[ 0.11849646 0.11396779 0.37025538 1.04053075 -1.51698273]]
[[ 1.76405235 0.40015721 0.97873798 2.2408932 1.86755799]
[-0.97727788 0.95008842 -0.15135721 -0.10321885 0.4105985 ]]
test_numpy_random_seed_order_further(randint_high=5)
1
[[ 1.12279492 0.30280522 0.07085926 0.07304142 -1.42232584]]
[[ 1.76405235 0.40015721 0.97873798 2.2408932 1.86755799]
[-0.97727788 0.95008842 -0.15135721 -0.10321885 0.4105985 ]]
緊接上面對隨機數(shù)干擾項對考察,我們看到,這次我們改變了干擾項隨機數(shù)生成器,np.random.randn(1, 5)的生成結果不同于test_numpy_random_seed_order中同一行的運行結果。
另外,兩次設置不同的randint的右邊界,np.random.randn(1, 5)生成的結果也全然不同,這說明了np.random.randint設置不同的參數(shù),即是全然不同的隨機數(shù)發(fā)生器。這一點,也不難在其他類型的隨機數(shù)分布中得到驗證。
到此這篇關于Python隨機數(shù)種子(random seed)的使用的文章就介紹到這了,更多相關Python隨機數(shù)種子內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python爬蟲使用正則爬取網(wǎng)站的實現(xiàn)
這篇文章主要介紹了python爬蟲使用正則爬取網(wǎng)站的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
Python?Flask實現(xiàn)后臺任務輕松構建高效API應用
本文介紹如何使用Python?Flask框架實現(xiàn)后臺任務,以快速構建高效的API應用。通過實例演示,讀者將學會如何利用Flask框架搭建后臺任務,實現(xiàn)異步處理和多線程操作等高級功能,提升應用性能和用戶體驗2023-04-04
python使用標準庫根據(jù)進程名如何獲取進程的pid詳解
Python有一套很有用的標準庫(standard library)。標準庫會隨著Python解釋器,一起安裝在你的電腦中的,所以下面這篇文章主要給大家介紹了關于python使用標準庫根據(jù)進程名如何獲取進程pid的相關資料,需要的朋友可以參考下。2017-10-10
flask 使用 flask_apscheduler 做定時循環(huán)任務的實現(xiàn)
這篇文章主要介紹了flask 使用 flask_apscheduler 做定時循環(huán)任務的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12

