random
Pseudo-random numbers from a Mersenne Twister generator. Module-level functions
use a shared generator in the current VM; Random creates independent state.
This generator is for simulations and games, not cryptographic secrets.
For weighted choices, supply finite nonnegative weights with matching length
and total greater than 1e-6; use a nonnegative k.
Unseeded generators initialize from the clock on first use.
Repeatable independent draws
from random import Random
rng = Random(7)
state = rng.getstate()
rolls = [rng.randint(1, 6) for _ in range(5)]
rng.setstate(state)
assert rolls == [rng.randint(1, 6) for _ in range(5)]
loot = rng.choices(['coin', 'gem'], weights=[9, 1], k=3)
assert len(loot) == 3
assert all([item in ['coin', 'gem'] for item in loot])
State bytes use pocketpy's internal representation, not CPython's state tuple. Do not assume sequences or saved states are interchangeable with CPython or across future runtime versions.
A generator can also be pickled:
import pickle
from random import Random
original = Random(7)
restored = pickle.loads(pickle.dumps(original))
assert original.random() == restored.random()