Skip to content

Give each game its own random.Random - #380

Open
taziksh wants to merge 5 commits into
bcollazo:mainfrom
taziksh:per-game-rng
Open

Give each game its own random.Random#380
taziksh wants to merge 5 commits into
bcollazo:mainfrom
taziksh:per-game-rng

Conversation

@taziksh

@taziksh taziksh commented Jul 23, 2026

Copy link
Copy Markdown

Problem

Game.__init__ calls random.seed(self.seed). The stream is shared by every game in the same process, so games running concurrently corrupt each other's draws and break reproducibility of seeds.

Change

  • Game creates self.random = random.Random(seed) and passes it to State, which uses it everywhere the module global was used before.
  • State.copy() and Game.copy() share the rng by reference, so simulations on copies advance the same stream, matching current behavior
  • Bots now draw from game.state.random, instead of the same global random
  • For backward compatibility with external callers, we include an optional rng parameter in roll_dice() and initialize_tiles that defaults to random
  • Multiprocessing playouts now give each playout its own deterministic child seed. Previously, every pool worker unpickled the same rng state, so all playouts were identical

Tests

We tested by replaying seeded games with AlphaBeta, MCTS, and random bots, and checking the logs are identical to main. We also added test_seeded_games_are_isolated_from_each_other (fails on main, passes here) and a playout-diversity test, and fixed test_gym_reproducibility and the discard sequence test, which relied on the old global seeding.

Game and State previously seeded the module-global random, so concurrent
games in one process corrupted each other's streams. State now owns a
Random(seed), shared by reference with state copies so simulations advance
the same stream. Streams are identical to the previous global behavior for
a given seed.
@netlify

netlify Bot commented Jul 23, 2026

Copy link
Copy Markdown

👷 Deploy request for catanatron-staging pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 89e9397

taziksh added 4 commits July 23, 2026 16:56
The test assumed WHITE sits after RED, but State shuffles seating and
the discard sequence advances by seat index without wrapping, so it
failed for about half the seatings. Pick roles by seat index instead.

@bcollazo bcollazo left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really cool! I'm a bit busy these days, but will try to review ASAP. If someone else can review / test that'd be great too!

Do games serialize nicely still to the Database? Does the GUI work nicely still?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants