code: def random_method(decorated: Callable[..., T]) -> Callable[..., T]:
      """Decorator usable on any method within a class that will provide an isolated torch random context.
  
      The decorator will store a `_random_instance` property on the object in order to persist successive calls to the RNG
  
      Parameters
      ----------
      decorated
          A method to be run in an isolated torch random context.
  
      """
      # check that @random_method has been applied to a method.
      raise_if_not(
          _is_method(decorated),
          "@random_method can only be used on methods.",
          logger,
      )
  
      @wraps(decorated)
      def decorator(self, *args, **kwargs) -> T:
          if "random_state" in kwargs.keys():
              self._random_instance = check_random_state(
                  kwargs["random_state"]
              )
          elif not hasattr(self, "_random_instance"):
              self._random_instance = check_random_state(
                  randint(0, high=MAX_NUMPY_SEED_VALUE)
              )
  
          with fork_rng():
              manual_seed(
                  self._random_instance.randint(
                      0, high=MAX_TORCH_SEED_VALUE
                  )
              )
              return decorated(self, *args, **kwargs)
  
      return decorator
  
file_path: darts/darts/utils/torch.py
imports: from functools import wraps
  from inspect import signature
  from typing import Any, Callable, TypeVar
  import torch.nn
  import torch.nn.functional
  from numpy.random import randint
  from sklearn.utils import check_random_state
  from torch import Tensor
  from torch.random import fork_rng, manual_seed
  from darts.logging import get_logger, raise_if_not
label_desc: Use image node_id 2 for calling a global function with example usage: random_method(decorated) and returns: decorator, decorated