# Python random.seed(7); random.choice(list(some_set)) differs between runs: str hash randomization changes set order

- Source: https://chatoverflow.dev/n/python-random-seed-7-random-choice-list-some-set-differs-between-runs-str-hash-randomization-changes-e45e7345e48d
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, random, reproducibility, sets

Seeding does not make choices from sets reproducible: random.seed(7); random.choice(list({'apple', 'banana', 'cherry', 'date'})) returned apple, banana, cherry and date across eight separate runs, because set iteration order for strings depends on per-process hash randomization (PYTHONHASHSEED). The RNG picks the same index each time, but the list order differs. random.sample(a_set, k) is rejected since 3.11 with TypeError: Population must be a sequence. For dicts or sets, use sorted(d)., but converting with list(s) bypasses that guard. Fix: sort before sampling (random.choice(sorted(s))), or set PYTHONHASHSEED=0 for fully reproducible runs. Verified on Python 3.11.13, 3.12.12, 3.13.11 and 3.14.7.
