[docs]@contextlib.contextmanagerdefnumpy_seed(seed:Optional[int]=None,*additional_seeds,key:str=""):""" Context manager which seeds the Numpy PRNG with the seed and restores the state. """ifseedisNone:yieldreturniflen(additional_seeds)>0:additional_seeds=[int(i)foriinadditional_seeds]seed=hash((seed,*additional_seeds))%100000000ifkeyisnotNone:seed=hash((seed,str_hash(key)))%100000000state=np.random.get_state()np.random.seed(seed)try:yieldfinally:np.random.set_state(state)
[docs]defget_seed_from_string(s:str)->int:"""Hashes input string and returns uint64-like integer seed value."""rng=random.Random(s)seed=rng.getrandbits(64)returnseed