Sub topic 3
Risk and reward, and how chance is controlled
Prerequisites: This chapter assumes you can compute probabilities with fractions and percentages, read simple Python, and understand how dice, cards, and loot drops work in games.
Risk and reward as a design tool
Expected value is the average payout of a random choice over many tries. A player who chooses a risky action should get a higher expected value than one who plays it safe, or the risk is not worth taking. Tune the payoff so the gap between risk and reward matches what you want players to feel.
Weighted random tables
A weighted table assigns each outcome a weight. The chance of an outcome is its weight divided by the total weight.
| Outcome | Weight | Chance |
|---|---|---|
| common | 70 | 70% |
| rare | 25 | 25% |
| epic | 5 | 5% |
import random
rng = random.Random(42)
table = {"common": 70, "rare": 25, "epic": 5}
drop = rng.choices(list(table), weights=list(table.values()), k=1)[0]
print(drop)Shuffle bags: randomness with guarantees
A shuffle bag removes each item after it is drawn and refills only when empty. Every item appears once per cycle, so long streaks of the same result cannot happen.
class ShuffleBag:
def __init__(self, items, rng):
self.items = list(items)
self.rng = rng
self.bag = []
def draw(self):
if not self.bag:
self.bag = self.items[:]
self.rng.shuffle(self.bag)
return self.bag.pop()Pity timers and bad-luck protection
A pity timer counts failed attempts and forces a success at a set limit. This caps how long a player can go without a reward.
def roll_with_pity(rng, state, chance=0.02, limit=50):
state["misses"] += 1
if state["misses"] >= limit or rng.random() < chance:
state["misses"] = 0
return True
return FalseMitigating bad luck with catch-up and recovery
Catch-up mechanics give a player a small boost after a run of losses. Keep the boost small and temporary, so skilled play still decides results.
Pseudo-random number generators and seeds
A pseudo-random number generator (PRNG) is an algorithm that produces numbers that look random but follow a fixed sequence from a starting value called a seed. The same seed always gives the same sequence, which makes replays and bug reports reproducible.
import random
a = random.Random(123)
b = random.Random(123)
print(a.randint(1, 6) == b.randint(1, 6)) # True> Do not use a PRNG seeded from the clock for a replay feature. Store the seed with the save file instead.
Testing your design with simulation
Run thousands of trials with a fixed seed to measure the real win rate of a design. Compare the result with your expected value before shipping.
import random
def win_rate(trials=10000, seed=1):
rng = random.Random(seed)
hits = sum(rng.random() < 0.3 for _ in range(trials))
return hits / trials
print(win_rate())Exercise
A pack has a 2% base chance per open, and the pity limit is 50. Roughly what chance does a player have of seeing a guaranteed reward within 50 opens?
Show answer
It is certain. The 50th attempt always succeeds, so the player never goes more than 49 misses in a row.