最近总有同学问看不懂 np.random.seed(0) 的意思,下面给大家梳理下。
[Python] 纯文本查看 复制代码
?
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | import numpy as np np.random.seed( 0 ) print (np.random.rand( 2 )) print (np.random.rand( 2 )) np.random.seed( 1 ) print (np.random.rand( 2 )) print (np.random.rand( 2 )) np.random.seed( 0 ) print (np.random.rand( 2 )) print (np.random.rand( 2 )) np.random.seed( 1 ) print (np.random.rand( 2 )) print (np.random.rand( 2 )) |
代码输出如下:
[] 纯文本查看 复制代码
?
1 2 3 4 5 6 7 8 | [ 0.5488135 0.71518937 ] [ 0.60276338 0.54488318 ] [ 0.417022 0.72032449 ] [ 1.14374817e - 04 3.02332573e - 01 ] [ 0.5488135 0.71518937 ] [ 0.60276338 0.54488318 ] [ 0.417022 0.72032449 ] [ 1.14374817e - 04 3.02332573e - 01 ] |
通过以上代码我们可以发现,每次设置随机种子seed(0)后的随机数,再次运行的时候,还是和第一次运行产生的随机数是一致的,设置新的随机种子seed(1)后又会重新产生新的随机数。