Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why `seed()` method isn't part of `randint()` function?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. Why seed isn’t part of randint function as a keyword argument?
  2. What’s the idea behind this way of creating Nupy‘s functions ?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading