question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global Random Seed

See original GitHub issue

Hi,

First and foremost thanks for a great package!

I have a short question about whether or not it is possible to set a global random seed for numba?

I have a main file which uses a bunch of numba functions defined in other files. Most of these external numba functions make use of np.random and I would thus like to set a global random seed.

I have tried setting os.environ['PYTHONHASHSEED'] = str(seed) in the main file, however, this does not work (the numba functions change output from run to run). Note that I am running the file through VS Code, so, as far as I know, I cannot set environmental variables in other ways.

If I define np.random.seed(seed) in each of the numba functions (and add the extra variable seed to the function), it works. This method, however, is cumbersome and does not seems like the optimal way.

I hope that you can help!

Cheers,

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
stuartarchibaldcommented, Jul 17, 2020

Thanks for the report, glad to hear you like Numba!

From memory, in Numba the RNG seeds are held in thread local storage, so if the code is single threaded it should be possible to call the seed setting function once at the start of the program and then it’ll be reproducible across runs. Example:

from numba import njit
import numpy as np

@njit
def set_seed(value):
    np.random.seed(value)


@njit
def foo():
    return np.random.random()

@njit
def bar():
    return np.random.random()

@njit
def baz():
    return np.random.random()

@njit
def call_all():
    return foo(), bar(), baz()

if __name__ == "__main__":
    set_seed(0)
    print(foo())
    print(bar())
    print(baz())
    print(call_all())

running this repeatedly:

$ python issue6002.py
0.5488135039273248
0.7151893663724195
0.6027633760716439
(0.5448831829968969, 0.4236547993389047, 0.6458941130666561)

$ python issue6002.py
0.5488135039273248
0.7151893663724195
0.6027633760716439
(0.5448831829968969, 0.4236547993389047, 0.6458941130666561)

$ python issue6002.py
0.5488135039273248
0.7151893663724195
0.6027633760716439
(0.5448831829968969, 0.4236547993389047, 0.6458941130666561)
0reactions
stuartarchibaldcommented, May 28, 2021

Thanks for you quick answer @stuartarchibald . It will then work on a PR in the next few days.

Great, thanks, much appreciated!

Read more comments on GitHub >

github_iconTop Results From Across the Web

set random seed programwide in python - Stack Overflow
According to Jon's answer, setting random.seed(n) , at the beginning of the main program will set the seed globally. Afterward to set seeds...
Read more >
Stop Using NumPy's Global Random Seed - Built In
Stop Using NumPy's Global Random Seed. Set random seeds for individual classes in Python, instead. Here's how. Written byHenri Woodcock.
Read more >
Python Random seed() Method - W3Schools
The seed() method is used to initialize the random number generator. The random number generator needs a number to start with (a seed...
Read more >
Python random.seed() function to initialize the pseudo-random ...
random seed () function to initialize the pseudo-random number generator in Python to get the deterministic random data you want.
Read more >
tf.random.set_seed | TensorFlow v2.11.0
Operations that rely on a random seed actually derive it from two seeds: the global and operation-level seeds. This sets the global seed....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found