Fire Emblem6 Rng Data

I’m sorry. But Matthis is incorrect in his explanation, and Teq’s change, while indeed “fixing” 100% misses, will also allow 0% hits to land (at a higher rate than 100% misses even).

I have read other people assume the same thing Matthis does, possibly because it is fairly easy to understand the issue in those terms, but it is wrong. The game does check correctly against the generated number for hits. The issue is a bit less obvious, and lies in the generated number itself. As Matthis suggested, 100 can be drawn, but the issue is that that shouldn’t ever happen at all. The number should range from 0 to 99.

The reason 100 can be drawn lies in the implementation of the formula that converts a number that ranges from 0 to 65535 (what the actual rng generates) to that number that should range from 0 to 99:

result = rand(0~65535) / 655;

(note: this division here is an integer division that rounds towards 0).

As you can maybe see, that isn’t a very sound way of doing the conversion - as any random number above 65500 would result in a 100 (that is an effective 36/65536 = 0.054931641% chance of rolling a 100 and 655/65536 = 0.999450684% chances of rolling any specific number in the 0 to 99 range). FE7 and onward would fix this by changing the formula to the following:

result = rand(0~65535) * 100 / 65536;

If you need this applied to FE6: at DF0 paste 64 21 48 43 00 0C ( mov r1, #100 ; mul r0, r1 ; lsr r0, #16 ) (untested).

I assume the reason why fe6 does this silly looking rand / 655 thing is because the code originally looked like rand / (65536 / 100) as it does look mathematically correct, but it isn’t because of integer division rounding.

16 Likes