Random number seed np.random.seed()

This function is to generate a set of fixed random numbers. This random number can be understood as an infinitely long array, and a parameter needs to be np.random.seed()set. This parameter can be understood as the specified seed (that is, its number).
For example: np.random.seed(10), it means that the tenth seed group is used, and different labels can also be used. This parameter is equivalent to giving your random number array a name. When you want to use a certain For a group, just input its label. The following example illustrates:

import numpy as np
for i in range(1,3):
	print(i)
    np.random.seed(1)
    a = np.random.rand(2,3)
    print(a)

#######################################
#结果如下
1
[[4.17022005e-01 7.20324493e-01 1.14374817e-04]
 [3.02332573e-01 1.46755891e-01 9.23385948e-02]]
2
[[4.17022005e-01 7.20324493e-01 1.14374817e-04]
 [3.02332573e-01 1.46755891e-01 9.23385948e-02]]

It can be seen that the random arrays generated twice are the same, because the same set of random numbers is used.

for i in range(1,3):
	print(i)
    np.random.seed(i)
    a = np.random.rand(2,3)
    print(a)

#######################################
#结果如下
1
[[4.17022005e-01 7.20324493e-01 1.14374817e-04]
 [3.02332573e-01 1.46755891e-01 9.23385948e-02]]
2
[[0.4359949  0.02592623 0.54966248]
 [0.43532239 0.4203678  0.33033482]]

It can be observed that the random array generated for the first time here is still consistent with the above, indicating that the seed array is fixed, that is, seed(1) is fixed, and similarly, the random number seed arrays of other labels are also fixed.
I use the seed with the same serial number under different program files, and found that the generated results are the same, even if the program is closed and restarted, it is still the same, so I think the result of the seed serial number is fixed? (Is it related to the computer? I hope someone who understands it can answer it)

Guess you like

Origin blog.csdn.net/qq_53312564/article/details/126331354