View Page Source

Revision (current)
Last Updated by adelikat 5 days ago
Back to Page

This page deals with the RNG mechanics of Pokemon Ruby/Sapphire/Emerald.

[TODO]

! Cycling method

The RNG cycling method is determined by the following formula:

 value <- value*[[0x41C64E6D]] + [[0x6073]]

where 'value' is an unsigned 32-bit number.

This cycling method is known as a [http://en.wikipedia.org/wiki/Linear_congruential_generator|linear congruential generator], and covers all 32-bit values. For randomization purposes, the game will only ever consider the top half of the value when deciding random actions.

! Cycling habits

The game tends to cycle the RNG once per frame outside of battle or in menus, and twice per frame in battle outside of menus (when the battlefield is visible).

If the game cycles more than that on a given frame, it is trying to determine a random action.

!!! Initialization

! Initial seed

For Ruby/Sapphire, [Forum/Posts/262235|this post] details how the game sets the initial seed based on the 'uid' (Unix time) of the movie file. If no movie file is running, it uses system clock instead. In particular, this method generates any initial seed of the form 0x0000????, where the ???? is any 4-digit hexadecimal number. In VBA, the initial seed is planted on frame 6.

Note: If the GBA clock (real-time clock) does not work, the game assumes it is always 2000 Jan. 1, 00:00:00 without changing, and the initial seed is 0x000005A0.

For Emerald, the initial seed is always 0x00000000 and is planted on frame 1. However, if you start a new game, there is a second initial seed that occurs after naming your character. It doubles as the visible trainer ID.

! Trainer ID number (Ruby/Sapphire).

The trainer ID number is determined near the beginning of the game. It is set after the Pokemon professor dismisses you into the world (your character sprite shrinks and fades into the black background), but before you appear in the moving truck.

The trainer ID number is a 32-bit value. The top half is the secret ID and the bottom half is the visible ID, which the game displays on the character status screen as your trainer ID.

When the game sets your ID number, it cycles the RNG 3 times on that frame.

 xxxxxxxx
 aaaaxxxx    secret ID: aaaa
 bbbbxxxx    visible ID: bbbb

The trainer ID number is at memory address 0x2024EAE.

! Trainer ID number (Emerald)

The trainer ID is determined in two separate parts: the visible ID and the secret ID.

The visible ID is determined after you leave the naming screen for your character. The game creates an initial seed with the same value at that point as well. It does so by taking the bottom half of the value at address 0x4000104 at the time.

Address 0x4000104 is an entropy byte, which continually increases and is affected by execution at every step. It is unpredictable. It begins running when you enter the naming screen and stops when you leave, when the visible ID is determined.

The secret ID is determined shortly before the character sprite disappears into the black background, after the professor dismisses you.

When the game sets your secret ID, it cycles the RNG 2 times on that frame.

 xxxxxxxx
 aaaaxxxx    secret ID: aaaa

!!! Stat generation

! Some set Pokemon

For some set Pokemon (not wild), the game will decide its stats by cycling the RNG 5 times on that frame. This applies to the following Pokemon:

* The starter, when it is selected.
* The legendary, when it does its cry.
* Wally's Zigzagoon for the tutorial battle, as soon as you leave the gym.

 xxxxxxxx
 aaaaxxxx
 bbbbxxxx   PID: bbbbaaaa
 ccccxxxx   cccc -> (bin) xrrrrrqq_qqqppppp
            HP IV: ppppp, Atk IV: qqqqq, Def IV: rrrrr
 ddddxxxx   dddd -> (bin) xuuuuutt_tttsssss
            Spe IV: sssss, SpAtk IV: ttttt, SpDef IV: uuuuu

! First tutorial wild Pokemon (Poochyena in Ruby/Sapphire)

For the wild Pokemon in the tutorial, the game decides its PID by cycling the RNG 7 times on that frame, and IVs by cycling 4 times on the next frame

 xxxxxxxx
 ????xxxx
 ????xxxx
 ????xxxx
 ????xxxx
 aaaaxxxx
 bbbbxxxx  PID: bbbbaaaa

next frame:

 xxxxxxxx
 xxxxxxxx
 ccccxxxx   cccc -> (bin) xrrrrrqq_qqqppppp
            HP IV: ppppp, Atk IV: qqqqq, Def IV: rrrrr
 ddddxxxx   dddd -> (bin) xuuuuutt_tttsssss
            Spe IV: sssss, SpAtk IV: ttttt, SpDef IV: uuuuu

! Ralts (tutorial battle)

When the game generates Ralts, it cycles the RNG at least 7 times, depending on the RNG values:

 xxxxxxxx

Tentative trainer ID and PID

 aaaaxxxx
 bbbbxxxx   tentative trainer ID: bbbbaaaa
 ccecxxxx
 ddddxxxx   tentative PID: ddddccec
            if e>7, set tentative trainer ID and PID as trainer ID and PID, do IVs
            otherwise, repeat tentative trainer ID and PID

(Note: The game is ensuring that the generated Ralts is male.)

IVs:

 ffffxxxx   ffff -> (bin) xrrrrrqq_qqqppppp
            HP IV: ppppp, Atk IV: qqqqq, Def IV: rrrrr
 ggggxxxx   gggg -> (bin) xuuuuutt_tttsssss
            Spe IV: sssss, SpAtk IV: ttttt, SpDef IV: uuuuu

The generated trainer ID has no bearing on any other trainer ID in the game.

! Wild Encounter (Ruby/Sapphire)

The game determines whether a wild encounter occurs when you have completely entered the grass tile, or finished turning in a grass tile.

When the game checks for a random encounter, the game will cycle the RNG 2 times (3 times if first entering grass). If a random encounter occurs, the game will cycle the RNG some number of times, depending on the random values.

Entering grass:

 xxxxxxxx
 ????xxxx
 aaaaxxxx   encounter: aaaa mod 2880 < 320     otherwise stop

Already in grass:

 xxxxxxxx
 aaaaxxxx   encounter: aaaa mod 2880 < 320     otherwise stop

Encounter:

 bbbbxxxx   species and level of pokemon: bbbb mod 100
 ????xxxx
 ccccxxxx   nature: cccc mod 25      do tentative PID

Tentative PID:

 ddddxxxx
 eeeexxxx   tentative PID: eeeedddd
            if eeeedddd mod 25 == nature, set PID as tentative PID and do IVs
            otherwise repeat tentative PID

IVs:

 ffffxxxx   ffff -> (bin) xrrrrrqq_qqqppppp
            HP IV: ppppp, Atk IV: qqqqq, Def IV: rrrrr
 ggggxxxx   gggg -> (bin) xuuuuutt_tttsssss
            Spe IV: sssss, SpAtk IV: ttttt, SpDef IV: uuuuu

All numbers for a particular species and level are together. This is not true in general for numbers for a particular species, or numbers for a particular level. The species and level depend on the area where the random encounter occurs.

! Wild encounter hold item (Ruby/Sapphire)

Whether a hold item is rewarded or not is determined separately from the rest of the wild encounter generation. It is determined right at the boundary between outside of battle, and battle (the end of fadeout).

On the frame that determines the occurrence of a hold item, the game cycles the RNG 6 times. This occurs even if the Pokemon is incapable of obtaining a held item.

 xxxxxxxx
 ????xxxx
 ????xxxx
 ????xxxx
 ????xxxx
 aaaaxxxx   5% hold item occurrence: aaaa mod 100 >=95

!!! Battle

! In battle: Quick Claw activation

The game determines whether Quick Claw worked right before the battle menu appears with "Fight", etc. It stores a random value (top half) in address 0x2024D1C (Ruby/Sapphire). The game will only read this value when a move is selected for that turn.

When the game rolls to store the value in that address and so determine Quick Claw, the game will cycle the RNG 3 times on that frame.

 xxxxxxxx
 xxxxxxxx
 aaaaxxxx     Quick Claw activate: aaaa < 0x3333

Statistically, this gives about 1/5 chance for Quick Claw to work.

The game will cycle the RNG to store this value regardless of whether anyone has Quick Claw. Any other uses for this value are unknown.

! In battle: Accuracy

The game determines attack accuracy right before it says "X used attack", if the attack does damage. If the attack does not do damage, the game determines it after it says "X used attack".

When the game rolls for attack accuracy, the game will cycle the RNG 3 times on that frame.

 xxxxxxxx
 xxxxxxxx
 aaaaxxxx   Attack hit: aaaa mod 100 < acc%     otherwise miss

! In battle: Critical hits

The game determines critical hits a few frames after the message "X used attack", provided that the attack does damage, and the attack did not miss, and it is not a tutorial battle where the game forbids critical hits.

When the game rolls for critical hits, the game will cycle the RNG 3 times on that frame.

 xxxxxxxx
 xxxxxxxx
 xxxaxxxx   Critical hit: a==0

This assumes a 1/16 chance of a critical hit. It does not describe situations where the critical hit ratio is raised to be greater than 1/16, or the target has the ability Battle Armor (no critical hits).

! In battle: Damage variation

For almost all attacks, the amount of damage that an attack does varies between about 85% and 100%. The game determines damage variation three frames after it checks for critical hit.

When the game rolls for damage variation, the game will cycle the RNG 3 times on that frame.

 xxxxxxxx
 xxxxxxxx
 xxxaxxxx    Damage variation, max: a==0   min: a==15

The lower the number, the more damage the attack does.

It is not known for certain if 0000 is the only value that does max damage (a property in the first two generations where only the "best" value yields max damage).

! In battle: Pickup ability

When possible, the game determines item pickup, if any, from Pickup ability at the end of battle, a few frames before the fadeout. It is required to have a Pokemon that has the Pickup ability that is holding nothing.

When the game rolls for item pickup, the game will cycle the RNG 3 or 4 times on that frame.

 xxxxxxxx
 xxxxxxxx
 aaaaxxxx    Get item: aaaa mod 10 == 0        otherwise stop
 bbbbxxxx    Item reward: bbbb mod 100

Ruby/Sapphire:

(Note: From [http://bulbapedia.bulbagarden.net/wiki/Pickup|extrapolation of information], trial, and guesswork. Not 100% verified).

||Value||Item||
|0-29|Super Potion|
|30-39|Full Heal|
|40-49|Ultra Ball|
|50-59|Rare Candy|
|60-69|Full Restore|
|70-79|Revive|
|80-89|Nugget|
|90-94|Protein|
|95-98|PP Up|
|99|King's Rock|

! In battle: Catching a Pokemon

When throwing a ball to catch a Pokemon, the game determines not only whether or not the Pokemon will be caught, but how many times the Pokeball will shake. It is determined right after "X used Y ball!"

The game uses a check value, and requires that random values pass the check four times. For more information, see [http://bulbapedia.bulbagarden.net/wiki/Catch_rate].

When the game rolls for catching a Pokemon, the game will cycle the RNG 3 to 6 times on that frame.

  xxxxxxxx
  xxxxxxxx
  aaaaxxxx    Shake 1: aaaa < checkvalue         otherwise stop
  bbbbxxxx    Shake 2: bbbb < checkvalue         otherwise stop
  ccccxxxx    Shake 3: cccc < checkvalue         otherwise stop
  ddddxxxx    Catch: dddd < checkvalue

! In battle: Metronome (Emerald)

To determine the move used by Metronome, the game cycles the RNG at least 3 times. It is unknown why the game cycles more than necessary.

  xxxxxxxx
  xxxxxxxx

move:
  xaaaxxxx    aaa -> (bin) xxxb_bbbbbbbb
              Metronome move: bbbbbbbbb + 1
              if >354 or some forbidden move, do move again

See [http://bulbapedia.bulbagarden.net/wiki/List_of_moves].

----

See also: [http://bulbapedia.bulbagarden.net/wiki/Pok%C3%A9mon_data_structure_in_Generation_III|Bulbapedia] for more information on game mechanics.