Skip to content

random

initialize_random(seed)

Initialize the random number generator with the given seed.

Parameters:

Name Type Description Default
seed int

The seed to initialize the random number generator with.

required
Source code in src/flowcean/utils/random.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def initialize_random(seed: int) -> None:
    """Initialize the random number generator with the given seed.

    Args:
        seed: The seed to initialize the random number generator with.
    """
    global rng  # noqa: PLW0603
    rng = np.random.default_rng(seed)
    random.seed(get_seed())
    pl.set_random_seed(get_seed())

    np.random.seed(get_seed())  # noqa: NPY002
    try:
        import torch

        torch.manual_seed(get_seed())
    except ModuleNotFoundError:
        pass
    logger.info("Initialized random number generator with seed %d", seed)

get_seed()

Generate a random seed.

Returns:

Type Description
int

A random seed.

Source code in src/flowcean/utils/random.py
33
34
35
36
37
38
39
def get_seed() -> int:
    """Generate a random seed.

    Returns:
        A random seed.
    """
    return int(rng.integers(2**32 - 1))