1

How can I set numpy.random.seed(0) on a global scale? It seems I have to reset the seed every time I call a np.random function.

import numpy as np

np.random.seed(0)
print(np.random.randint(0,10,2))
np.random.seed(0)
print(np.random.randint(0,10,2))
print(np.random.randint(0,10,2))

np.random.seed(0)
print(np.random.rand())
np.random.seed(0)
print(np.random.rand())
print(np.random.rand())


[5 0]
[5 0]
[3 3]
0.5488135039273248
0.5488135039273248
0.7151893663724195
Andi
  • 3,196
  • 2
  • 24
  • 44
  • 1
    if you want fixed numbers what is the point in using random? – pygri Oct 22 '20 at 07:39
  • 1
    This may answer your question : https://stackoverflow.com/a/21494630/14280520 – lauriane.g Oct 22 '20 at 07:44
  • @lauriane.g I am actually wondering how to avoid resetting the seed every time I call a ``np.random`` function. – Andi Oct 22 '20 at 07:47
  • 1
    if you could answer as to why you think that you need to reset it, then we would have a chance to help you – pygri Oct 22 '20 at 07:48
  • 2
    There is almost no conceivable use case for what you're asking for - you are almost certainly making a mistake. Why do you think you need this? – user2357112 Oct 22 '20 at 07:52
  • I need to create a bunch reproducible random arrays. If I don't set the seed, I do get different outcomes each time I run the code. This makes unit testing quite complicated. Also, others can't reproduce my results. – Andi Oct 22 '20 at 07:57
  • 1
    You're taking the wrong approach. The thing you're asking for would break all code that uses NumPy random routines anywhere in your program, including stuff you didn't write and didn't want to affect, and stuff you *did* write that wasn't related to this part. Rather than trying to set the seed "on a global scale", you should do it *locally*, by using your own instance of `numpy.random.RandomState` (or better, `numpy.random.Generator` in new code). – user2357112 Oct 22 '20 at 08:06
  • Having your routines take a seed (or an RNG) as an argument would be much better for testability than breaking random number generation across the entire program. – user2357112 Oct 22 '20 at 08:08
  • @user2357112supportsMonica Fair enough. Now let's suppose my toy code is a method that takes the seed as input argument. Then my question is still valid, you just change "global" for "local". How can I set the seed for all ``np.random`` functions within this particular method? – Andi Oct 22 '20 at 08:12
  • 1
    You don't. The point was reproducibility, and what you're asking for wouldn't help with reproducibility. Create a seeded RNG once at the start of the function and let the seed advance. Two calls to `your_rng.rand` within the same run of your function won't produce the same result, but the sequence of results will be the same across runs with the same seed. – user2357112 Oct 22 '20 at 08:18
  • If you need two copies of the same random array in a single run of your function, create a random array and then copy it, rather than calling the RNG twice. – user2357112 Oct 22 '20 at 08:18
  • OK, now I got it. Thanks for clarifying! – Andi Oct 22 '20 at 08:25
  • 1
    I’m voting to close this question because the underlying problem is better solved another way – Sam Mason Oct 22 '20 at 09:53

1 Answers1

2

That is how seeds actually work. You set a 'seed' value which determines all following generated random numbers. You can think of the seed as a starting point for randomly generated numbers. Every time you set the seed you set a starting point for generating a random sequence.

Every time the code generates a random number, it steps 'forward' from the seed/starting point in a random (but deterministic) way. Setting the seed puts the random number generator in a specific state from which it will follow the same random path every time (due to the is the deterministic character).

Andre
  • 760
  • 3
  • 13
  • Your explanation is correct. However, this doesn't answer the question. I would like to avoid to set the seed every time I run a ``np.random`` function. I want to set it globally. The question is if this is possible at all? – Andi Oct 22 '20 at 08:00
  • As others have pointed out, that is a common way to use a seed, but to answer your question no don't know if that is possible. One way to work around is to generate a set of random numbers once at the start of your code, and store them in a variable/list. Then every next time you need them you refer to this list. – Andre Oct 22 '20 at 08:09