Repository navigation
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.
-
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 multiplier1812433253. Two details differ from the reference code: a seed of0is replaced by19650218, and a generator used before any seeding initialises itself with5489. - The engine keeps one instance,
s_rand, insrc/libs/monolib_mm/source/util/ml_rand.cpp. Everyml::mtRand*function reads it:-
ml::mtRand()returns the 32-bit output shifted right by one (0 to 2^31 - 1). -
ml::mtRand(n)returnsmtRand() % n(0 whennis 0);ml::mtRand(min, max)returnsmtRand() % (max - min) + min(minwhen the range is empty). The modulo bias is negligible for the smallnthe game uses. -
ml::mtRandf1()returns the 32-bit output times1 / 4294967295(0.0 to 1.0 inclusive);mtRandf2()uses0.999999 / 4294967295so 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 asgf::GfBladeManager::getRandom(max)andgetRandomF1()(mtRand(max)andmtRandf2()).
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.
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):
-
Static initialisation.
s_randis constructed withnn::os::GetSystemTick().GetInt64Value()(ml_rand.cpp), truncated to 32 bits by theu32seed parameter. -
Framework start-up.
fw::Framework::initialize(0x710017ad84, not decompiled yet; the call is at 0x710017afe8) readsml::DevClock::getClockNowand, when the clock device is active, callsml::mtInitwith its low 32 bits. On the SwitchgetClockNowis againGetSystemTick()(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.
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.
| 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) |
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.