Random number generation#
In order to make your simulations reproducible, it is important to configure a
random seed which makes your code deterministic. When Sionna is loaded, the
Config singleton instantiates random number generators (RNGs) for Python,
NumPy, and
PyTorch. You
can then set a single seed which will make all of your
results deterministic, as long as only these RNGs are used. In the cell below,
you can see how seed is set and how
py_rng,
np_rng, and torch_rng() can be used in your
code. All of Sionna PHY’s built-in functions rely on these RNGs.
import torch
from sionna.phy import config
config.seed = 40
# Python RNG - use instead of
# import random
# random.randint(0, 10)
print(config.py_rng.randint(0, 10))
# NumPy RNG - use instead of
# import numpy as np
# np.random.randint(0, 10)
print(config.np_rng.integers(0, 10))
# PyTorch RNG - use instead of
# torch.randint(0, 10, (1,))
print(torch.randint(0, 10, (1,), generator=config.torch_rng(), device=config.device))
7
5
tensor([7])
For code that uses torch.compile, use the compile-aware utilities in the
utility functions section of the PHY API (e.g. randint(),
normal()) and pass generator=config.torch_rng()
in eager mode; they automatically switch to the global RNG when compiled.