Seed AI RNG deterministically to prevent replay desync#1956
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.
There is no AI in replays. So how exactly have you seen errors? |
| } | ||
| } | ||
| auto prng = std::mt19937(std::random_device()()); | ||
| auto& prng = AI::getRandomGenerator(); |
There was a problem hiding this comment.
This one looks correct though
In my own fork I use ai-battle with a small eval framework to improve the AI: |
|
Can you explain a bit what you are doing there so I don't have to search your code? Because replays replay recorded actions. So there is no AI running there doing actions and hence no possibility for asyncs in replays. Note that this gives a false impression of safety: You seed the AI at the start. Then you save the game. The AI actions from the continued game at that point and from the loaded game won't match. |
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.