Random number generator

From Boktai Hacking Wiki

The GBA Boktai games use a very simple method of generating random numbers:

  1. There's a list of 1024 pregenerated u16 random numbers stored in EWRAM ("RNG Table")
  2. The game keeps track of the last list index used in IWRAM ("RNG Index")
  3. When the game needs a random number, it increments the RNG index (mod 1024)...
  4. ...and then returns RNG_Table[RNG_Index].
  5. When the game needs a random value within a certain range, it instead returns (RNG_Table[RNG_Index] >> 3) % upper_bound (unknown why the game does a right shift/division by 8 here).

These calulations are inlined everywhere, there's no dedicated rand() function. Boktai 1/2 use the same RNG table, Boktai 3 (maybe?) uses a different one, TODO. The RNG table never changes. The quality and period of this RNG is very bad.

Bootstrap RNG

The RNG table is generated by a Linear congruential generator on reset. This LCG has the formula seed = seed*0x5d588b65 + 1, with an initial seed of 123456 (maybe the default rand() implementation of the GBA SDK/libc?).

After generating the RNG table, the initial RNG index is also generated from this LCG.

Because this LCG is only used immediately after booting the game, and never used during active gameplay, it's called the "Bootstrap RNG".

TODO: Verify this page for Boktai 3

See also