5

Like in R, I would like to set a random seed globally for the entire script/session, instead of having to call the random seed function every time I execute a function or run a model. I am aware that sci-kit learn uses the numpy RNG, but also could not find a way to set it globally.

I have read several posts here on this topic, such as this one: Differences between numpy.random and random.random in Python

It explains the difference between the two RNG classes, but not how to set it globally.

Is there no way of doing this except for calling the random seed EVERY time I want the output to be the same?

## Random Library

import random
##### Random seed given
random.seed(42)
print(random.random()) #will generate a random number 

##### No seed given
print(random.random()) #will generate a random number 

##### Random seed given
random.seed(42)
print(random.random()) #will generate a random number 


#############################

## Numpy Library

import numpy as np

##### Random seed given
np.random.seed(42)
print(np.random.random())

##### No seed given
print(np.random.random())

##### Same seed given
np.random.seed(42)
print(np.random.random())
Wurtzinator
  • 61
  • 1
  • 1
  • 4
  • That's not the point of seeding an RNG, and it's not what seeding the RNG does in R. – user2357112 Apr 25 '19 at 10:03
  • The point of setting a fixed RNG seed is to get the same results on every run of the program, not to get the same result from every RNG call made within a single run of the program. – user2357112 Apr 25 '19 at 10:08
  • I understand that this may not be common usage, but it would help me in my case. You cannot count on everyone in a team to set the correct seed everytime they run a function or a script. Providing them with a piece of code to implement at the begining of every script that sets a seed globally simplifies things. – Wurtzinator Apr 26 '19 at 08:43
  • Except that you still haven't understood the implications of what you're asking for. You can't do anything statistically meaningful with a die that always lands on 4, and you can't do anything statistically meaningful with an RNG that resets to the same seed on every call. Sure, it's useful to be able to call the same simulation function twice with the same seed and get the same result, but what you're asking for would reset the seed on every RNG call within the simulation too. There's no option that will automatically know when it needs to reset the seed and when to leave it alone. – user2357112 Apr 26 '19 at 16:30
  • The solution here is to get better at seed management - and while you're at it, you should really be using a local RNG rather than messing with the global seed. – user2357112 Apr 26 '19 at 16:31

1 Answers1

-2

Your question looks contrary to whole idea of random number generator(valid in case for getting deterministic results).Generally, you want to seed your random number generator with some value that will change each execution (or different number eg. set some cookie no for a session) of the program. For instance, the current time is a frequently-used seed. The reason why this doesn't happen automatically is so that if you want, you can provide a specific seed to get a deterministic sequence.

Coming back to your question, if you want to have global seed and wanted to generate random with that seed. Then you can have a function to club both things and call whenever you want.

def same_seed_random()
     np.random.seed(42)
     print(np.random.random())

I would encourage you to check this for more on random seed@ https://pynative.com/python-random-seed/

  • 1
    You have the default backward - both `random` and `numpy.random` default to a seeding mechanism expected to produce different results on every run. C's `rand` defaults to a set seed of 1, but C's `rand` is pretty terrible in general. The point of seeding the RNG manually in Python is usually to produce deterministic results, the opposite of what you say. – user2357112 Apr 25 '19 at 10:06
  • Hi BlacFurry. Thank you for your reply! Yes, indeed it seems contrary, but that is exactly what I need. – Wurtzinator Apr 25 '19 at 14:07
  • Sorry, last comment was not complete. Thank you for your reply! Yes, indeed it seems contrary, but that is exactly what I need. In our team we are sharing code and try to re-run models using the same settings to easily spot increased performance between iterations. There are also a few other pseudo-random selections made that create subsets of data which we would like to remain the same. If I see correctly, you are suggesting that I simply run my entire script in a function and specify the seed in that function as this way seed will remain the same for the scope of the function? – Wurtzinator Apr 25 '19 at 14:13
  • yeah and this looks like a use case to me.if your are engaged in code sharing with your team, then there is one thing you can take away. i would recommend you to create a module for this function and share among your team. This is make your project modeler and extendable. – BlackFurry May 01 '19 at 16:38
  • 4
    for reproduction you regularly need to set the random seed fixed – dreamflasher Oct 02 '19 at 08:32
  • Consider using `np.random.RandomState` to create local random state, as opposed to resorting to side effects / manipulating global state. – trianta2 Jan 07 '22 at 04:05