Toggle Background Color

Resources: The Inner Workings of the Random Number Generator

Just a disclaimer that there may be several things I have missed, as I didn't look super deeply into this, and I may add more to this update as more discoveries are made.

Random number generators, or RNG for short, are pretty much in just about every video game to introduce randomness in various factors of the gameplay or even just doing something mundane like making visual effects less repetitive. There are a bunch of other resources that can explain this concept far better and in more detail, but the basic rundown is that computers can't actually generate random numbers. They can only simulate such a task. So how people went about faking randomness in computers was to essentially come up with a math formula that would continuously generate a sequence of numbers that seems random.

Of course said formula or whatever other techniques used to make such a sequence has to start from somewhere. The value that kicks this off is known as a seed. Generally that's also where the biggest part of the randomness comes into play and helps disguise the fact that random number generators truly aren't random is by starting off with a different seed each time so the sequence of events that results from it is hard to replicate as possible, but if you somehow started out from the same seed from another play session, you would find that the events would play out the same time if you followed the same triggers as the other session.

That does leave a bit of a chicken and egg situation where the starting seed would have to be random in order to induce randomness. A common technique to work around that limitation is to fetch the current date and time at the time on the system upon booting the software, and then converting it to a value to start off with essentially a random seed, as time constantly moves forward and each moment is different from the last. Of course generally nothing is stopping you from just setting the time and date to the same value as another session to start off with the same seed.

7th Dragon does not do this. Instead all of the random number generators it uses has the same starting seed of 0. It only tries to disguise this by triggering the RNG roll, also known as advancing the RNG, several times before settling on a starting value. And even then the value is consistent on booting. (Though the starting value can be different between different emulators for some reason.) This pretty much means you can be stuck in a predictable sequence of events if you're on the lookout for them, and if you know how to, you can exploit this to get the results you want.

The game actually has multiple RNGs it makes use of during the game, not just 1.



The game has 5 RNG values it uses during the game, all of which are 4 bytes long and are unsigned values. That means the number cannot be negative, and the value caps at a maximum of 4,294,967,295, or 0xFFFFFFFF in hexadecimal. The 4 addresses that are seen here are where the RNG values for the first 4 RNGs are located in the game's memory. The 5th one on the other hand is only loaded during battle, and its location is shuffled around quite a bit, which is why it's not listed here.

Now as for the formula used for advancing the RNG sequence, the following is used for all of the random number generators in the game. The randomly generated number is run through this formula every time the RNG is called and used for a function, and this can happen every so often, or several times within one frame.

RNG = (RNG * 1,566,083,941) + 2,531,011

The resulting value is cut off at the last 4 bytes in hexadecimal, with all digits past that being completely ignored. If the RNG is equal to 0 (Like at startup), it'll just add the value of 2,531,011 and continue on its merry way.

If the game only needs a 2 byte value to work with, it has a couple of methods for truncating the generated 4 byte value. It will either take the generated value and add 1 to it, and then perform a modulo operation on the number with an operand of 65535, or 0xFFFF in hexadecimal. This means that the number is divided by 65535, but the actual result of the operation is ignored. The remainder of the value is what the game cares about. Since the remainder can't be equal to 65535, as that would set the remainder to 0, it can be used as a way to effectively shorten the range of the random number rolled for.

Alternatively the game just shifts the RNG value in binary to the right 16 times in to shorten the value to a 2 byte value. If you're wondering what that means...

Imagine 32 people on a mountaintop. All of them have information that adds up into a blob that you can't exactly decipher in that state. So you ask them to organize into a line from left to right, each with a bit of information you can actually work with. The first 16 people from the left all have the bits you want. You care not for the other 16 people on the right side of the line, as they are all useless. So in order to get rid of them, you go to the left side of the line, and shove the line 16 spaces to the right, which causes them to fall off the mountain's peak to their doom. You can now ask the remaining 16 people for the information they have, and proceed to convert that back into into one big blob of information that's relevant to the task at hand.
And then after you're done with that information, you shove the remaining 16 people off the peak to make a decorative crater at the mountain's base. You can then repeat the process with the next set of 32 people.
...Couldn't you have explained that in a less morbid way?
Why couldn't you just have asked the first 16 people for the information in the first place!?
Because computers are jackasses.

At any rate, here are the functions each of the listed RNG values are used for, and how often they're advanced.

RNG 1: Handles most of the gameplay RNG that happens on the field. This RNG is called for generating special Bloom tiles, generating the encounter meter, determining what direction NPCs and dragons will walk in if they're set to random movement, and what formation of monsters you'll run into on a random encounter. This RNG is easily the most static of the bunch, as it is the only RNG value that is not rolled several times on booting the game, and that the events that call for the RNG only occur under certain conditions and only advance the RNG once for each trigger.

The actions that advance it are getting into a battle or getting out of battle, triggering a screen transition such as going into a town our out into the overworld (using a portal to teleport to another portal doesn't count), generating Bloom tiles from going into a Bloom infested area or using a Bloom Seed, and the actions of NPCs which dragons also fall under. This includes changing directions, though their idle animations can also advance the RNG.

RNG 2: I have absolutely no idea what this is even used for. It's advanced quite a bit on boot and then stops. But past that point it never seemed to change or be used for anything as far as I could tell.

RNG 3: Handles graphical effects such as particle effects and sprite animations. Not advanced on boot, only once the game is loaded. This constantly advances during the field, though is mostly paused during battles. During battles it only really advances if the character uses an action with an animation, or an enemy dies and they spawn particle effects as the sprite disappears from the battle.

RNG 4: Another primarily gameplay related random number generator. Primarily used in combat related functions that have random outcomes, such as accuracy, and infliction rates. This also includes preemptives and surprise attacks, as well as item drop rates. Entirely static in the field, though it's advanced for quite a bit on booting and is paused until combat is initiated, at which point it's advanced once whenever an action that requires the use of RNG is called.

RNG 5: No idea what this is used for. Only loaded during battle, and is constantly advancing. Once the battle ends, it falls into disuse or outright removed from the game's memory until the next battle.

Because RNG 1 and 4 are fairly static until called and control some of the more important parts of the game, this actually opens them up a lot to RNG manipulation if you know what you're doing. Though only a few methods for manipulating RNG 1 are known at this time. Manipulating RNG 1 and 4 would basically be theorycrafting at this point.