You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
705 B
29 lines
705 B
2 years ago
|
import numpy as np
|
||
|
import hashlib, struct
|
||
|
|
||
|
_current_seed = 0
|
||
|
|
||
|
|
||
|
def set_seed(seed):
|
||
|
"""
|
||
|
Set the random seed to be used globally. If a string is supplied, it
|
||
|
will be converted to int using hash().
|
||
|
|
||
|
This immediately seeds the numpy RNG. Any other RNGs must be seeded using
|
||
|
current_seed()
|
||
|
"""
|
||
|
if isinstance(seed, str):
|
||
|
seed = struct.unpack("=I", hashlib.md5(seed.encode("utf-8")).digest()[:4])[0]
|
||
|
np.random.seed(seed)
|
||
|
assert seed < 2 ** 64 # neuron RNG fails if seed is too large
|
||
|
global _current_seed
|
||
|
_current_seed = seed
|
||
|
return seed
|
||
|
|
||
|
|
||
|
def current_seed():
|
||
|
"""
|
||
|
Return the currently-set global random seed.
|
||
|
"""
|
||
|
return _current_seed
|