**HP and MP:**Damage_taken = max(CurHP_at_battle_start - CurHP_at_battle_end, 0)

if Damage_taken == 0, bail (no chance to gain HP)

else Damage_quotient = MaxHP / Damage_taken

if random(0 to 8) >= Damage_quotient, gain HP.

MP uses exactly the same algorithm

The upshot is you need to lose more than 1/9 of your maximum HP or MP to have any chance of gaining, but above that minimum, the probability quickly grows asymptotically towards 100%.

**Strength:**if random(0 to 45) < times_Fight_selected, gain Strength

The counter for the number of times the character selected Fight is the same one used for gaining weapon skills, so the select-cancel trick works.

**Intellect:**if random(0 to 25) < times_Black_Magic_selected, gain Intellect

**Spirit:**if random(0 to 15) < times_White_Magic_selected, gain Spirit

It's easier to gain Spirit than Intellect, perhaps to compensate for the fact that white magic spells are often cast out of battle. Either that or black magic is just supposed to be "harder" than white magic in the world of FF2.

**Losing Intellect/Vitality/Strength:**When you gain Strength, Intellect or Spirit, you have a chance to lose Intellect, Vitality or Strength respectively:

if random(0 to 5) == 0, lose the corresponding stat

This check is done immediately after the gain check. Theoretically, the chance of losing a point is only ~10% (considering the abovementioned RNG quirk) but in practice it's much higher. This is because in FF2's RNG table, the numbers between 0 and 25 mostly occur immediately after other low numbers. Since the game only checks for losing a point after you've successfully gained one, and since gaining a stat requires drawing a low number from the RNG, the check for losing a point is more likely to draw another low number and succeed than the algorithm suggests.

Incidentally, unlike FF1, FF2's battle RNG table isn't located anywhere in the ROM. It's generated algorithmically (which is why it's so biased--the algorithm is really crude) and stored in RAM at the start of each battle. Presumably they did it this way to save ROM space, but it's a bit silly, since the ROM already contains a RNG table that's used for random encounters and out-of-battle healing. Why didn't they just use that table for battle results as well? FFL uses an algorithmically-generated RNG table for everything, and the reason clearly

*is* to save ROM space, as the FFL ROM is completely packed.

**Agility:**if (Evasion / 4) > 0 and random(0 to 255) <= (Evasion / 4), gain Agility

The chance of gaining Agility isn't just affected by Evasion, it's directly proportional to Evasion. A rather dubious algorithm, given that Evasion is based on Agility. The more Agility you have, the easier it is to gain more!

**Vitality and Magic:**Reuses the "damage quotients" that were calculated for HP/MP.

if random(0 to 9) > HP_damage_quotient, gain Vitality

if random(0 to 9) > MP_damage_quotient, gain Magic

The minimum amount of HP or MP you have to lose is the same as for gaining HP/MP, but the chance of gaining is slightly lower--it starts at 1/18 and goes up by 1/9ths, rather than starting at 1/16 and going up by 1/8ths.

Any sufficiently advanced incompetence is indistinguishable from malice.