Random seed

Utilities for seeding the random number generators.



Seed the random number generators


API

skrl.utils.set_seed(seed: int | None = None, deterministic: bool = False) int

Set the seed for the random number generators

Note

In distributed runs, the worker/process seed will be incremented (counting from the defined value) according to its rank

Warning

Due to NumPy’s legacy seeding constraint the seed must be between 0 and 2**32 - 1. Otherwise a NumPy exception (ValueError: Seed must be between 0 and 2**32 - 1) will be raised

Modified packages:

  • random

  • numpy

  • torch (if available)

  • jax (skrl’s PRNG key: config.jax.key)

Example:

# fixed seed
>>> from skrl.utils import set_seed
>>> set_seed(42)
[skrl:INFO] Seed: 42
42

# random seed
>>> from skrl.utils import set_seed
>>> set_seed()
[skrl:INFO] Seed: 1776118066
1776118066

# enable deterministic. The following environment variables should be established:
# - CUDA 10.1: CUDA_LAUNCH_BLOCKING=1
# - CUDA 10.2 or later: CUBLAS_WORKSPACE_CONFIG=:16:8 or CUBLAS_WORKSPACE_CONFIG=:4096:8
>>> from skrl.utils import set_seed
>>> set_seed(42, deterministic=True)
[skrl:INFO] Seed: 42
[skrl:WARNING] PyTorch/cuDNN deterministic algorithms are enabled. This may affect performance
42
Parameters:
  • seed (int, optional) – The seed to set. Is None, a random seed will be generated (default: None)

  • deterministic (bool, optional) – Whether PyTorch is configured to use deterministic algorithms (default: False). The following environment variables should be established for CUDA 10.1 (CUDA_LAUNCH_BLOCKING=1) and for CUDA 10.2 or later (CUBLAS_WORKSPACE_CONFIG=:16:8 or CUBLAS_WORKSPACE_CONFIG=:4096:8). See PyTorch Reproducibility for details

Returns:

Seed

Return type:

int