Skip to content

no-repeated-unseeded-stdlib-random-in-test

A collected test may repeatedly sample an unseeded standard-library PRNG.

Why

Repeated unseeded sampling creates unreproducible generated inputs, making failures harder to replay and debug when sampled values affect behavior.

Fix

Use an isolated deterministic generator such as rng = random.Random(17), inject one, or use a property framework that records failing seeds.

Examples

Before — flagged Do not repeat probabilistic trials without reproducibility
tests/test_selector.py
import random
def test_distribution():
values = [random.random() for _ in range(50)]
assert min(values) < 0.5
After — preferred Make repeated random sampling reproducible
tests/test_selector.py
import random
def test_distribution():
rng = random.Random(17)
values = [rng.random() for _ in range(50)]
assert min(values) < 0.5

Formerly: uncontrolled-randomness-in-test