What Is Going on Inside Their Head: the Pokémon Emerald Random Number Generator
Forbidden Tempura 6/16/2024In Pokémon Emerald, there are several random number generators. The main one, pp_rand(), uses the following generator: seed = (1103515245 * seed + 24691) as u32 return (seed >> 16) as u16 Where do these constants come from? The 1103515245 constant is easily explained: It is what ISO/IEC 9899 in all editions provides as an example for how to implement rand(), which itself traces back to 70s UNIX libc rand.c. That function returns a signed 16-bit integer with RAND_MAX == 32767. Game Freak, however, wanted to have an unsigned 16-bit integer. Therefore, they doubled RAND_MAX to be 65535 instead of 32767. Unsure if this was safe, they also doubled the increment from 12345 to 24690 and added 1 to make sure it was relatively prime to 1103515245, as evidenced by the following comment in calctool.c: // 12345*2+1 (RAND_MAX=32727の時に12345なら65535の時は倍必要なのではないか?テスト) In other words, Game Freak was unsure if they also needed to double the increment when doubling RAND_MAX, displaying a failure t
