Random Number Generator Mastering for the Save/Load Scum

Sid Meier's Civilization V4 · 43 ratings4.3k views59 favorites9 min readby HolyswordUpdated Sep 3, 2021View on Steam ↗

Introduction

Civ5 gives you a lot of information about the modifiers in a battle and even gives you a prediction of how much damage you should inflict/receive. Sometimes you find yourself trusting these numbers and counting with killing an enemy unit, but that doesn't happen: upon attacking you will notice that the result deviates from this prediction very often and may completely jeopardize your plans.

I've heard people saying that these disparities arise because of the modifiers, but that's absolutely not true: the damage prediction already accounts for the modifiers. In fact, in this very combat you could inflict 27 or 35 damage, and receive 25 or 31 damage... and the one to blame is this RNG dude.

If you've played with save/loading a lot, you also probably noticed that the outcome of battles depends strongly on the order that they are made, i.e. reloading the game and repeating your actions in exactly the same order gives exactly the same outcome. So... is it random or not?

Who is RNG?

Computers are unable to generate "true randomness". The best they can do is to generate a sequence of numbers so chaotic and unpredictable, that they "act random" -that's what algorithms called "random number generators" are for. Those algorithms usually rely on a "random seed" which determines the "random sequence". For instance, assume that you start a RNG with the seed 1234 and requested five random numbers, obtaining [1,8,2,0,0]. Now you start it again with the seed 1235 and the five random numbers you get are [5,9,1,4,3]... in other words: completely different numbers. If you restart the RNG with the seed 1234 again, you will once again get [1,8,2,0,0]. Every time you restart this RNG with this seed, it will give you exactly these five numbers.

Strictly speaking, it is mathematically possible to predict the next number from any RNG algorithm (and some people have been exploiting e-Casinos like that sometimes); however, it is so damn difficult (and for the purposes of Civ5, quite of an overkill) that you might as well just play fair.

But what does this mean for Civ5?

Every time you save the game, the RNG saves its state (i.e. the seed and the position). Every time you reload, you are restarting the RNG. Whenever Civ5 needs a new random number, it will take it always from the same sequence, in the same order. As far as I know, Civ5 "consumes" random numbers in the following ways:
  • ranged combat: 1 number
  • melee combat: 2 numbers (atk and def) plus a mysterious 3rd number
  • combat against civilians: 0 numbers!
  • great people: 1 number (to decide in which city they spawn)
  • ancient ruins: 1 number
  • pillaging tile: 2 numbers
  • conquering a city: 2 numbers (atk and def) + an unknown amount of numbers to determine which buildings will be destroyed
  • ???
  • profit
PS: I'd be more than happy to discuss the list above. They are the product of extensive trial-and-error and as such, is prone to mistakes!

You cannot control which number Civ5 will take, but you can understand the order and what it implies to you as a player. And THAT, my friends, is where the fun begins...

Ranged Combat

By far the most useful use of understanding and tinkering with the RNG is improving your chances in combat. The best way to explain how this work is by doing it yourself; let us start with ranged attacks which are much simpler to understand.

  1. open a map where you already have lots of ranged units or cities with enemies within range
  2. move anything you want to move, then save; don't ever move anything again, lest you change the modifiers (e.g. flanking bonus, different terrain, etc)
  3. note down the predicted damage of the attack
  4. attack and note the outcome (DO NOT KILL any enemy unit! I'll explain later in the section!)
  5. repeat 3-4 this for as many units as you want; you'll end up with a table of prediction/outcome.
  6. compute the difference between each prediction/outcome, divide by the prediction and multiply by 100: voilá, you got the relative difference in percentage

This relative difference in percentage is going to be your sauce. I call it random modifier or RM; notice it can be negative or positive.

Now load the game again (which you save on step 2) and repeat steps 3-5 but... in a different order... Unsurprisingly, the RMs are approximately the same as well! Approximately, I said, because some infamous variations may occur. Small RM sometimes flip around because of the way damage is computed (e.g. a +5% RM might become a -5%) but by far your worst enemy are the Indels: a random number was used as a side effect of one of your attacks, and you didn't register it. This causes an apparent "shift" in the sequence of the RMs. I have a small section about this problem later in this guide.

Also notice that attacking civilian units (e.g. a Worker) with a ranged attack will *not* consume a random number! They have 0 defence and always take exactly the predicted damage.

The method is not foolproof but it is surely better than completely throwing yourself onto the arms of Chaos. You'll be able to save the good RM for important attacks and use the negative RM for eliminating practically dead units, or even better: you can throw the bad RMs to your enemies during melee attacks.

Melee Combat

My suggestion is to always start with ranged attacks in order to get reliable RMs; then reload and use those RMs between ranged and melee as you wish.
The thing you must bear in mind is that melee attacks consume 2~3 RMs:
  1. for your atk
  2. for the enemy's atk
  3. (???) for pleasing the lords of anarchy
In other words, a melee attack with RMs [+15%, +20%, +5%] will give both you and the enemy a bonus. Much better would be to perform a melee attack with the RMs [+15%, -20%, -5%]: that way your unit gains a bonus, the enemy's get a penalty and the random deity gets a more or less irrelevant modifier.

Quite honestly, the 3rd random number puzzles me. I am not sure what it is used for, but my current speculation is that it comes from abilities such as "Battle Enrollment". Some units are able to "'recruit" a defeated enemy with a certain probability. This surely consumes a random number. However, the 3rd random number is consumed even if neither of the units in the combat possess such ability.

"Accursed Indels!" and Other Shenanigans

Death of a Unit
Previously I advised you to not kill units when determining the RMs list. The reasons are:
  • if you kill a land unit, the damage you cause is capped by its health, e.g. a predicted attack of 40 on unit with 5HP will cause 5 damage. Therefore, you just cannot get any information about the RMs when killing a land unit
  • naval units are even worse; they just say "lol is u ded"
  • sometimes, deaths cause Indels or Reset (see below)
────────────────────────────────────────────────────────
Indels
tl;dr: your RMs list has a "shift". You cannot avoid it, and you can only detect those after extensive save/loading.

Indel is a portmanteau word for insertion/deletion - a very common term in string search algorithms.
They are exactly what they sound like: an insertion or a deletion of an item in a list. Assume that you get RMs [1,2,3,4,5], reload, and then you get RMs [1,2,0,3,4]. With these numbers it is quite obvious what is going on: the 0 was not there before, but now it is. The truth is: the 0 was always there, it is just that something happened off the screen after the 2 and you couldn't notice it.

Sadly, when this happens in game it is usually not that obvious. For instance, with [1,2,3,3,4] and you would probably not even notice the difference until much later into the sequence. This gets worse if it happens when you kill a unit (and thus you don't see the damage).

There is a way to have an Indel "detector" and it is an algorithm called Needleman-Wunsch; this algorithm is used to align DNA sequences, considering that nucleotides might have been inserted or deleted along the course of evolution. It is not difficult to implement, but requires exactly two lists of RMs. If you have more lists, you can use Carillo-Lipman algorithm for multiple sequences alignment but... then, come again, how important is it to figure out exactly where the indels are?

According to my experiments, the following things are the usual culprit behind an indel:
  • spawning of great generals/admirals: 1 random number is consumed to determine which city they spawn
  • death of a unit: 1 random number for prayers and thoughts (sometimes)

It is easy to detect and indels caused by great generals/admirals spawning: if the indel occurs only after you attack a specific Player, it is likely a Great Person being spawned by that Player. ────────────────────────────────────────────────────────
Resets
Even with careful examination, sometimes you can't see the pattern; the RMs just change completely after one attack, or the indels are so complicated that you might as well just consider everything from the scratch - a "reset" if you will.

Resets are deterministic (i.e. they will occur always at the same place, if you do things in the exact same order) and the sequence that follows a reset also follows the same pattern explained here.

Final Remarks

All in all, the method (kinda) works with several caveats. Dealing with the shifts caused by indels is much of a hassle most of the time. Moreover, you cannot just get good random modifiers out of nothing - you can just use them if they are there. So, here are some final remarks:

  • according to my observations, the random modifiers vary roughly between -25% and +25% in steps of 5%
  • ranged attacks follow pretty neatly the pattern explained here; it is not 100% but surely helps a lot
  • it is a good strategy to use a bad RM to kill a practically dead unit; it effectively wastes the bad modifier, but sometimes it shifts things too, or completely resets the sequence. Be aware of that!
  • this method does not teach you how to make good decisions in the game - you can still suck at it to be fair

Quite frankly, only few scenarios are so hard that require deep understanding of those things. Take this as a tool to use in very specific situations. erm, I'm looking at you, Pax Romana Aeternum Build your own spreadsheet that keeps track of the RMs, and predict the outcome of the next battle. Overusing this will be so cumbersome and distracting that it might spoil the fun. In the end, you will not be free from save/reload strategy, but you'll be able to min/max it to some extent.

I wish you all lots of fun while staring at the loading screen!

This guide was created by its original author on the Steam Community. Are you the author and want it removed? Request removal.