Sub topic 12
Shuffle bags: randomness with guarantees
Assume you know basic probability, can read simple Python, and understand independent random rolls. This chapter focuses on a shuffle bag, a system that draws from a fixed collection without replacement and refills it when empty.
Why use a shuffle bag?
Independent random rolls can produce any sequence that the probabilities allow. If a reward has a 20% chance on each attempt, five failures in a row are possible. The chance of a reward does not increase after each failure.
A shuffle bag changes the short-term behavior. Suppose a bag contains four hit entries and six miss entries. Each entry is shuffled once. Drawing removes an entry, so the bag contains fewer possible results after every draw. When the bag is empty, it is refilled and shuffled again.
This gives a guarantee over each complete bag:
- Exactly four of ten draws are hits.
- Exactly six of ten draws are misses.
- The order remains unpredictable.
- A refill starts a new random order.
| System | What is guaranteed? | Typical use |
|---|---|---|
| Independent rolls | Long-run average only | Critical hits, loot drops |
| Shuffle bag | Fixed count per bag | Card draws, encounter pacing |
| Pity timer | A success by a limit | Rare rewards |
A shuffle bag does not prevent every streak. With four hits and six misses, six misses can still appear first. It limits the total number of misses in one bag, but not the length of every possible run.
A simple implementation
Duplicate entries represent weights. Four copies of hit and six copies of miss produce a 40% hit rate per complete bag.
import random
class ShuffleBag:
def __init__(self, items, rng=None):
if not items:
raise ValueError("items must not be empty")
self.items = list(items)
self.rng = rng or random.Random()
self.remaining = []
def draw(self):
if not self.remaining:
self.remaining = self.items[:]
self.rng.shuffle(self.remaining)
return self.remaining.pop()
rng = random.Random(7)
bag = ShuffleBag(
["hit", "hit", "hit", "hit", "miss", "miss",
"miss", "miss", "miss", "miss"],
rng,
)
results = [bag.draw() for _ in range(10)]
print(results)
print("hits:", results.count("hit"))The first ten draws contain exactly four hits, although their positions vary. The seeded generator makes this example repeatable for tests and replays.
Designing the bag
Choose the bag contents from the experience you want, not only from the average probability.
- Add duplicate entries to increase an outcome's frequency.
- Use separate bags for unrelated systems, such as combat and rewards.
- Decide whether the bag persists between rounds, encounters, or matches.
- Save the remaining entries when the game must resume exactly.
- Avoid silently rebuilding the bag after every draw. That turns it into independent sampling.
- Avoid changing the bag during a cycle unless the change is part of the design.
A shuffle bag works well when players should feel that outcomes are random but should not suffer unlimited bad luck. It is less suitable when every attempt must be statistically independent, such as a simulation of a physical process.
Exercise
A bag contains two wins and eight losses. Can the first three draws all be losses, and what is guaranteed after ten draws?
Show answer
Yes. The first three draws can be losses. After ten draws, exactly two draws are wins and eight are losses.