Falsebound Archive
5 Bugs & quirks 5.10

Random numbers that are never seeded

Every random outcome of the game (damage, critical hits, dodges, the computer player’s choices, level-up growth, enemy rosters, traders, script rolls) comes from one generator, HSD_Rand. No code gives it a seed: it starts at 1 at every power-on. What makes it vary is time. The screen-refresh interrupt steps it once per refresh, which is twice per game frame, and every value the game draws steps it once more.

Where
main.dol, HSD_Rand (0x8008596C); retrace callback vi_retrace_callback (0x8009D970)
Seeded
Never; the state starts at 1
Steps
Once per screen refresh (twice a game frame) and once per value drawn
Steps before the first frame
1,378 (the start rosters’ draws and the start-up refreshes)Unconfirmed
On the map, idle
Nothing drawn; hovering a base with a cannon draws 3 values a frame
State in memory
u32 at 0x802849B8

The generator is the usual linear one: state = state × 214013 + 2531011, and a draw returns the top 16 bits (0 to 65535). Before the main loop starts, the game draws values to build the start rosters, and the screen refreshes of start-up step it too. In the Dolphin emulator the first frame began from the same state, 0xC8A65D63, in every logged run: 1,378 steps from the start value.

From then on the state at any moment depends only on the number of frames since power-on and on the values the game itself has drawn. The same inputs on the same frames give the same battles, the same level-ups and the same enemy armies; one frame of delay before an action moves every later roll by two steps.

While the map is in play and nothing happens, the game draws nothing, so waiting is the only thing that moves the generator. One exception: while the pointer is on a base that has a cannon, the range ring takes its colour from three random values a frame, so each frame there costs five steps instead of two.

Load times do not break the pattern: the game loads in the background, and in the logged runs every frame took exactly two refreshes, the map load included. A saved game does not hold the generator; after a load it goes on from whatever state the frames since power-on have given.

Two other generators exist and change no outcome: the C library’s rand, also never seeded and also stepped every refresh, drives only visual effects (the map’s sparks and the attack effects), and the sound driver seeds its own from the console’s timer. That one is drawn only by the sequence player’s random-play commands, which no sound of the game uses.

Reading it in an emulator

The state is the big-endian 32-bit word at 0x802849B8 (the value after the last step); the C library’s state is at 0x80284700. Dolphin’s memory view or watch can show it while the game runs.

Evidence

HSD_Rand and every call to it were read in the original machine code, and none of them sets the state. The refresh callback vi_retrace_callback steps it and the C library’s state once per call. The main loop waits until the refresh count is a multiple of two, which makes a game frame two refreshes. In the Dolphin emulator, with a small patch that logs the state once a frame, 3,316 frames from power-on (opening movie, title screen and a battle demo) and 2,519 frames of a map started from the developer menu each took exactly two refreshes, and the first frame began from 0xC8A65D63, as the code predicts. The map’s three draws a frame come from its range-ring drawing code.