np.random.seed(0)
defines that the random generation of numbers will follow a predefined pattern.
In doing np.random.seed(0)
, the next call from numpy.random.rand(n)
, being n
the size of the vector to be generated will follow a fixed pattern.
For example, without setting a Seed, equal calls from numpy.random.rand(n)
would return different results.
>>> numpy.random.rand(5)
array([0.79172504, 0.52889492, 0.56804456, 0.92559664, 0.07103606])
>>> numpy.random.rand(5)
array([0.64589411, 0.43758721, 0.891773 , 0.96366276, 0.38344152])
But when setting the Seed before calling numpy.random.rand(n)
, you ensure that the generated sequence will be equal / will follow the same pattern every time.
>>> numpy.random.seed(0)
>>> numpy.random.rand(5)
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])
>>> numpy.random.seed(0)
>>> numpy.random.rand(5)
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])
Note that when calling numpy.random.rand(5)
, a Seed is reset, it is necessary to arrow it novamento with numpy.random.seed(0)
.
The method
seed()
serves to start the pseudorandom number generator. Documentation: https://docs.python.org/3/library/random.html– Augusto Vasques
Possible duplicate of How computer randomization is generated?
– fernandosavio