It seems that I don't know enough about Numpy
random methods and functions. Never seen a method that is linked to the function like in this simple example:
seed(4)
randint(0,10,10)
...where seed()
is called by randint()
I'd like to know:
- Why seed isn't part of
randint
function as a keyword argument? - What's the idea behind this way of creating
Nupy
's functions ?
CodePudding user response:
A seed is meant to determine a sequence of RNG results. Like this:
In [1]: import numpy
In [2]: numpy.random.seed(4)
In [3]: numpy.random.randint(0, 10, 10)
Out[3]: array([7, 5, 1, 8, 7, 8, 2, 9, 7, 7])
In [4]: numpy.random.randint(0, 10, 10)
Out[4]: array([7, 9, 8, 4, 2, 6, 4, 3, 0, 7])
In [5]: numpy.random.randint(0, 10, 10)
Out[5]: array([5, 5, 9, 6, 6, 8, 2, 5, 8, 1])
In [6]: numpy.random.randint(0, 10, 10)
Out[6]: array([2, 7, 0, 8, 3, 1, 0, 3, 2, 3])
In [7]: numpy.random.seed(4)
In [8]: numpy.random.randint(0, 10, 10)
Out[8]: array([7, 5, 1, 8, 7, 8, 2, 9, 7, 7])
In [9]: numpy.random.randint(0, 10, 10)
Out[9]: array([7, 9, 8, 4, 2, 6, 4, 3, 0, 7])
In [10]: numpy.random.randint(0, 10, 10)
Out[10]: array([5, 5, 9, 6, 6, 8, 2, 5, 8, 1])
In [11]: numpy.random.randint(0, 10, 10)
Out[11]: array([2, 7, 0, 8, 3, 1, 0, 3, 2, 3])
See how after the second seed
call (on line In [7]
), the sequence resets?
When you set a seed, the RNG output still has the same statistical properties, but you can run the program again with the same seed and get the same results. This is useful for things like debugging, or reproducible simulations.
If seed
were part of randint
, that would reset the sequence every time. It would look like this:
In [12]: numpy.random.seed(4)
In [13]: numpy.random.randint(0, 10, 10)
Out[13]: array([7, 5, 1, 8, 7, 8, 2, 9, 7, 7])
In [14]: numpy.random.seed(4)
In [15]: numpy.random.randint(0, 10, 10)
Out[15]: array([7, 5, 1, 8, 7, 8, 2, 9, 7, 7])
In [16]: numpy.random.seed(4)
In [17]: numpy.random.randint(0, 10, 10)
Out[17]: array([7, 5, 1, 8, 7, 8, 2, 9, 7, 7])
In [18]: numpy.random.seed(4)
In [19]: numpy.random.randint(0, 10, 10)
Out[19]: array([7, 5, 1, 8, 7, 8, 2, 9, 7, 7])
Same results on every single call. Producing the same results on every call is not how we want RNG output to behave.
CodePudding user response:
randint
is not the only random number/sequence generator in numpy, there are other plethora of random functions, and passing seed everytime to different functions within the program doesn't make much sense, instead, you initialize the seed state once, and then it is used for all random functions.