In 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 to understand the theory of the algorithm they were employing. They did understand enough theory to realize that the multiplier is the most important part but then failed to see it through to the very end: As noted by Knuth in The Art of Computer Programming, vol. 2, the only thing that matters is that the multiplier and the increment are relatively prime. At least their change did not make it any worse. They also note that this is a test, yet it was evidently shipped in production.
Incidentally, 1103515245 is a terrible multiplier (K. Entacher, “A collection of selected pseudorandom number generators with linear structures”, Technical report series, ACPC - Austrian Center for Parallel Computation, August 1997).
