Random number generator: Difference between revisions

From Boktai Hacking Wiki
(Created page with "The GBA Boktai games use a very simple method of generating random numbers: # There's a list of 1024 pregenerated u16 random numbers stored in EWRAM ("RNG Table") # The game keeps track of the last list index used in IWRAM ("RNG Index") # When the game needs a random number, it increments the RNG index (mod 1024)... # ...and then returns <code>RNG_Table[RNG_Index]</code>. # When the game needs a random value within a certain range, it instead returns <code>(RNG_Table[RN...")
 
No edit summary
 
Line 17: Line 17:


''TODO'': Verify this page for Boktai 3
''TODO'': Verify this page for Boktai 3
= See also =
* [[RNG Table]]


[[Category:Documentation]]
[[Category:Documentation]]
[[Category:Boktai 1]] [[Category:Boktai 2]] [[Category:Boktai 3]]
[[Category:Boktai 1]] [[Category:Boktai 2]] [[Category:Boktai 3]]

Latest revision as of 21:03, 16 October 2024

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