Skip to content

Random Numbers

Kevin Bonnoron edited this page Nov 21, 2026 · 1 revision

Random Numbers

How the game produces random numbers, how the generator is seeded, and what that means for anyone who would like to predict or steer a random outcome (the rare Blade group, drops, Blade creation...).

Summary: almost every random draw of the game comes from one shared Mersenne Twister, seeded from the console's system tick counter when the framework starts, and stirred every frame by a number of extra draws taken from the frame's own timing. Random outcomes cannot be steered by input timing.

The generator

  • mm::mtl::MTRand (include/mm_mtl/mt_rand.h) is a standard 32-bit Mersenne Twister (MT19937): 624-word state, twist offset 397, the usual tempering masks (0x9d2c5680, 0xefc60000) and initialisation multiplier 1812433253. Two details differ from the reference code: a seed of 0 is replaced by 19650218, and a generator used before any seeding initialises itself with 5489.
  • The engine keeps one instance, s_rand, in src/libs/monolib_mm/source/util/ml_rand.cpp. Every ml::mtRand* function reads it:
    • ml::mtRand() returns the 32-bit output shifted right by one (0 to 2^31 - 1).
    • ml::mtRand(n) returns mtRand() % n (0 when n is 0); ml::mtRand(min, max) returns mtRand() % (max - min) + min (min when the range is empty). The modulo bias is negligible for the small n the game uses.
    • ml::mtRandf1() returns the 32-bit output times 1 / 4294967295 (0.0 to 1.0 inclusive); mtRandf2() uses 0.999999 / 4294967295 so that 1.0 is never reached; mtRandf3() sets the lowest bit first so that 0.0 is never returned. Each has (max) and (min, max) variants.
  • Game code reaches it directly (ml::mtRand(...)) or through thin wrappers such as gf::GfBladeManager::getRandom(max) and getRandomF1() (mtRand(max) and mtRandf2()).

Other generators exist but serve other subsystems: a static sRand inside anm::rand() (animation library) and ssd::XorShift128Random with its own SetSeed (sound library). They are not covered here.

Seeding

The shared generator is seeded twice, both times from nn::os::GetSystemTick(), the console's free-running tick counter (19.2 MHz on the Switch, about 52 ns per tick, counting since the console was powered on):

  1. Static initialisation. s_rand is constructed with nn::os::GetSystemTick().GetInt64Value() (ml_rand.cpp), truncated to 32 bits by the u32 seed parameter.
  2. Framework start-up. fw::Framework::initialize (0x710017ad84, not decompiled yet; the call is at 0x710017afe8) reads ml::DevClock::getClockNow and, when the clock device is active, calls ml::mtInit with its low 32 bits. On the Switch getClockNow is again GetSystemTick() (ml::DevClockNx::getClockNowSys, src/libs/monolib_mm/source/device/nx/dev_clock_nx.cpp).

The only other callers of ml::mtInit in the executable are two debug displays (event::BgmManager::render, event::CsSndManager::render), which seed with 0 while drawing a sound-level bar graph.

Consequences:

  • The seed is not the console's date or time, so changing the system clock does not change it.
  • The tick advances 19.2 million times per second, so reproducing a seed would require starting the game with a precision of tens of nanoseconds.
  • The low 32 bits of the tick wrap every 2^32 / 19.2 MHz, about 3 minutes 44 seconds, so in practice the seed is uniformly distributed.

Per-frame stirring

At the end of every gf::GfManager::update (src/application/BF2/gf/gf_manager.cpp), the game measures how long the frame's update took with an ml::TimeCount (in ticks) and then calls ml::mtRandf1() (elapsed ticks & 0xf) times, discarding the results. Each frame therefore burns between 0 and 15 extra numbers, chosen by the low bits of the frame's own duration, which vary with CPU load and scheduling.

On top of that, every system that draws a number advances the same generator (see below), so the position in the sequence at any moment depends on everything that happened since start-up.

Examples of what the generator decides

Outcome Draw Code
Rare Blade group of a save (1 to 5), drawn once at new game mtRand(5) + 1 gf::GfBladeManager::setupGame (see Blade-Creation)
Kizuna Ring reward set BLD_CreateConfig row + mtRand(4) GfKizunaringUtil::getCompleteKizunaringReward (see Drivers-Blades-Party)
Number of mobs in a crowd group mtRand(PopMin, PopMax + 1) GmkMob (see Field-Gimmicks)
Random NPC talk line mtRand(count) event::RandCalc (see Event-System)
Blink interval of a character in a cutscene mtRand(60) + 90 frames event::CharaWinkPlayer (see Event-System)

What this means for prediction and manipulation

The rare Blade group of a save cannot be chosen by timing inputs or by setting the console clock:

  • The seed comes from a nanosecond-scale tick counter.
  • Every frame between start-up and the new-game draw consumes a variable number of values, through the per-frame stirring and the other systems.

Retrying a new game until the wanted group appears remains the only way to pick it. Some outcomes are rolled once and stored in the save, like the Blade group in SDataGame (see Save-Data); reloading a save does not reroll those.

Clone this wiki locally