
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
secretsmodule instead.randomis predictable on purpose.
Assignment
Use a random.Random(seed) object inside each function:
roll_dice(seed, count): return a list ofcountrolls (1-6).pick_crew(names, k, seed): returnkdifferent names.shuffled(items, seed): return a shuffled copy. The original list must not change.
Use a random.Random(seed) object inside each function:
roll_dice(seed, count): return a list ofcountrolls (1-6).pick_crew(names, k, seed): returnkdifferent names.shuffled(items, seed): return a shuffled copy. The original list must not change.
main.py Python sleeping
Press Run to try your code, then Check to test it.