delu.random.preserve_state

delu.random.preserve_state()[source]

Decorator and a context manager for preserving global random state.

Examples

import random

f = lambda: (
    random.randint(0, 10),
    np.random.randint(10),
    torch.randint(10, (1,)).item()
)
with preserve_state():
    a = f()
b = f()
assert a == b

@preserve_state()
def g():
    return f()

with preserve_state():
    a = g()
b = g()
assert a == b