random with a seed

random makes pseudo-random numbers: randint(a, b) (both ends included), random() (a float from 0 up to 1), choice(xs), sample(xs, k) (k different items) and shuffle(xs) (in place).

The numbers come from a formula started by a seed. Same seed, same sequence, every run. That's what you want in tests and simulations: results you can repeat and debug.

Prefer rng = random.Random(42) over random.seed(42). It's your own generator, so other code calling random can't change your sequence.

Random numbers are also good for quick simulations. Below, rolling two dice many times estimates the chance of a 7, which is 1 in 6.

For passwords or tokens, use the secrets module instead. random is predictable by design.

Run it twice and check you get the same output. Then change the seed.