Seed AI RNG deterministically to prevent replay desync#1956
Open
FlexApex wants to merge 1 commit into
Open
Conversation
The AI used two separate non-deterministic random sources: - AI::getRandomGenerator() in random.cpp was seeded with std::random_device on first use, so it started at a different state in replay mode than during the original game run, causing AI decisions to diverge - CheckSeaAttack in AIPlayerJH.cpp allocated its own std::mt19937 seeded with std::random_device()() every call, doubly breaking determinism Fix: seed AI::getRandomGenerator() from the game's random_init at StartGame (same point where RANDOM.Init is called), so replay and live game use the same AI RNG sequence. Replace the local mt19937 with the shared AI generator.
Member
There is no AI in replays. So how exactly have you seen errors? |
Flamefire
reviewed
Jun 29, 2026
| } | ||
| } | ||
| auto prng = std::mt19937(std::random_device()()); | ||
| auto& prng = AI::getRandomGenerator(); |
Member
There was a problem hiding this comment.
This one looks correct though
Author
In my own fork I use ai-battle with a small eval framework to improve the AI: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The AI uses a non-deterministic random source, which breaks replay determinism. Two distinct issues:
AI::getRandomGenerator()is never seeded from the game seed. It is lazily seeded fromstd::random_deviceon first use, so in replay mode it starts from a different state than during the original match. Any AI decision that draws from it diverges, desyncing the replay.AIPlayerJH::TrySeaAttack()allocates its own generator —std::mt19937(std::random_device()())— on every call, which is non-deterministic by construction.The net effect: any replay in which an AI evaluates a sea attack desyncs ("The played replay is not in sync with the original match"), even though the simulation is otherwise correct.
Fix
AI::getRandomGenerator()fromrandom_initinGameClient::StartGame(), right whereRANDOM.Init(random_init)is already called — so replay and live runs share the same AI RNG sequence.std::mt19937inTrySeaAttack()with the sharedAI::getRandomGenerator().3 lines across 2 files; no behavioural change to a live game beyond making the AI RNG reproducible.
How it was found
Surfaced while running headless replay verification on AI-battle recordings (related to the headless replay player in #1950) — a desync verifier flagged AI sea-attack frames as the first point of divergence, with the RNG checksum being the differing field.