Code

Dice of Fate

The random module makes games unpredictable:

import random

random.randint(1, 6)          # 1..6, BOTH ends included
random.choice(["oil", "map"]) # one random item
random.sample(crew, 2)        # 2 different items, as a new list
random.shuffle(cards)         # shuffles IN PLACE, returns None

Seeds make randomness repeatable

These numbers are pseudo-random: a formula produces them from a starting seed. Same seed means the same sequence, which is perfect for tests, replays and "daily challenge" levels.

rng = random.Random(42)   # your own generator, seeded with 42
print(rng.randint(1, 6), rng.randint(1, 6))  # same every run

A separate random.Random(seed) object doesn't disturb anyone else using the global random functions.

Pip tip: For passwords or secret tokens, use the secrets module instead. random is predictable on purpose.

Assignment

Use a random.Random(seed) object inside each function:

  1. roll_dice(seed, count): return a list of count rolls (1-6).
  2. pick_crew(names, k, seed): return k different names.
  3. shuffled(items, seed): return a shuffled copy. The original list must not change.