"""Utilities targeting random number generation."""importcontextlibimportrandomimportwarningsimportnumpyasnpfromtyping_extensionsimportdeprecated
[docs]@deprecated("Using 'set_random_seed' is deprecated and support will be removed in a future ""release. Use the new settings management system instead. For details: ""https://emdgroup.github.io/baybe/stable/userguide/settings.html)",)defset_random_seed(seed:int):"""Set the global random seed. Args: seed: The chosen global random seed. """importtorch# Ensure seed limitsseed%=2**32torch.manual_seed(seed)random.seed(seed)np.random.seed(seed)
[docs]@deprecated("Using 'temporary_seed' is deprecated and support will be removed in a future ""release. Use the new settings management system instead. For details: ""https://emdgroup.github.io/baybe/stable/userguide/settings.html)",)@contextlib.contextmanagerdeftemporary_seed(seed:int):# noqa: DOC402, DOC404"""Context manager for setting a temporary random seed. Args: seed: The chosen random seed. """importtorch# Ensure seed limitsseed%=2**32# Collect the current RNG statesstate_builtin=random.getstate()state_np=np.random.get_state()state_torch=torch.get_rng_state()# Set the requested seedwithwarnings.catch_warnings():warnings.simplefilter("ignore",DeprecationWarning)set_random_seed(seed)# Run the context-specific codetry:yield# Restore the original RNG statesfinally:random.setstate(state_builtin)np.random.set_state(state_np)torch.set_rng_state(state_torch)