| The Process of PID and IV Creation of Non-Bred Pokemon
CreditsBefore I even start, I need to give credit to loadingNOW (a.k.a. pika) and yamipoli for providing me with invaluable information regarding this topic. PreliminariesWe start by providing preliminary information, without which the reader will have a very hard time understanding this article. The Binary SystemIn a computer, numbers are not stored normally, but in a format called binary. The numbers we normally use are said to be in the decimal system. Every number in the decimal system is written as a series of digits, each of which can be one of the following ten: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. In the binary system, the same thing is true, but there are only two possible digits: 0 and 1. Each of these binary digits is called a bit (short for binary digit). For example, the binary number 10001110 has 8 bits. The Game Boy Advance and Nintendo DS systems, on which the Pokemon games Ruby, Sapphire, Emerald, Fire Red, Leaf Green, Diamond and Pearl run, are, in effect, small computers, and thus also utilise binary numbers. The Pokemon games, however, have an extra simplification: they always use non-negative whole numbers only. This makes our discussion of binary numbers easier. How do we interpret a binary number? To do this, let’s think for a moment about how we interpret decimal numbers. What does the number 635, say, mean? It means a number that has 5 units, 3 tens (= 1 × 10) and 6 hundreds (= 10 × 10) added together. Notice that the value of a digit in the decimal system is ten times as big as that of the digit immediately to the right of it. So the number 635 really means 600 + 30 + 5. The same thing happens in binary, except that the value of a digit in the binary system is twice as big as that of the bit immediately to the right of it, not ten times as big. Let’s provide an example. Say we need to interpret the binary number 10001110 as a number in the decimal (i.e. normal) system. We have 0 units, 1 twos (= 1 × 2), 1 fours (= 2 × 2), 1 eights (= 4 × 2), 0 sixteens (= 8 × 2), 0 thirty-twos (= 16 × 2), 0 sixty-fours (= 32 × 2) and 1 one-hundred-and-twenty-eights (= 64 × 2). Thus, the binary number 10001110 is equal to 2 + 4 + 8 + 128 = 142. Another example: let’s interpret the 12-bit binary number 101110010101 as a number in decimal. It is equal to 1 units, 0 twos, 1 fours, 0 eights, 1 sixteens, 0 thirty-twos, 0 sixty-fours, 1 one-hundred-and-twenty-eights, 1 two-hundred-and-fifty-sixes (= 128 × 2), 1 five-hundred-and-twelves (= 256 × 2), 0 one-thousand-and-twenty-fours (= 512 × 2) and 1 two-thousand-and-forty-eights (= 1024 × 2). Hence it is equal to 1 + 4 + 16 + 128 + 256 + 512 + 2048 = 2965. The Hexadecimal SystemBinary numbers tend to have quite a large number of digits. A way to write binary numbers in a shorter way is the hexadecimal system. In the hexadecimal system, a binary number is first grouped into groups of four bits each. If the number of bits in the binary number is not divisible by 4, extra 0 bits are added at the start of the binary number so that the number of digits is divisible by 4. Then each group of four bits is replaced by a symbol as follows:
For example, the binary number 10001110 is written in hexadecimal as 8E. The first 4 bits are 1000, written as 8 in hexadecimal, while the last 4 bits are 1110, written as E. The binary number 101110010101 is written as B95 in hexadecimal (1011 = B, 1001 = 9, 0101 = 5). The binary number 1110000001 has 10 bits. We first add two zeros at the beginning so that it has 12 bits: 001110000001. Then we convert it to hexadecimal as 381 (0011 = 3, 1000 = 8, 0001 = 1). Of course, we can also convert hexadecimal numbers to binary numbers easily by doing the reverse process. For example, the hexadecimal number 5AF7 is equal to 0101101011110111 in binary (5 = 0101, A = 1010, F = 1111, 7 = 0111). From now on, a hexadecimal number will be written surrounded by square brackets [] so as not to possibly confuse it with a decimal number. This is because the hexadecimal number [4680] is a different number from the decimal number 4680. What is a PID?Whenever a Pokemon is created in the games, the first thing that is generated is a 32-bit number called a PID (Pokemon IDentification number). This number is sometimes also called the Personality Value of a Pokemon, and is not visible anywhere in the game. It can only be found by looking into the Pokemon save file... or by an applet that can be located here. A lot of information about the Pokemon can be found using just the PID alone. In particular, the nature of a Pokemon is found just from its PID. Where applicable, the gender, ability and Unown letter shape are also found just from the PID of the Pokemon in question. The Pokemon Random Number GeneratorWhenever a random event occurs in the Pokemon games, and indeed in the majority of games, the randomness of the event is not truly random, but is governed by a mathematical formula that generates so-called pseudo-random numbers. When we say pseudo-random, we mean that the numbers generated are not truly random numbers, but are sort-of fake random numbers. There are various methods that can be used to generate pseudo-random numbers. One of the simplest types of random number generators is the class of linear congruential random number generators. Many computer applications adopt this method of random number generation, as, while it is very simple to implement, it produces good random numbers when given particular values. By "good random numbers" we mean that if the numbers were to be listed next to each other, we wouldn’t have a clue as to what the next pseudo-random number would be in the list unless we apply the formula. The random number generator (RNG) used in all the Pokemon games from Ruby and Sapphire onwards works as follows. When the game loads, the program assigns a number to a 32-bit variable which we shall call seed. The way this is done varies from game to game (you can read loadingNOW’s article for more information). Then, whenever the random number generator is invoked, the following steps are executed:
Thus, as you can see, the Pokemon RNG produces pseudo-random 16-bit numbers, i.e. numbers between 0 and 65535 (or between [0000] and [FFFF]). For instance, given the seed [1A56B091], what is the random number that the above RNG outputs? First we need to multiply [1A56B091] by [41C64E6D]. Using a calculator, the answer of this multiplication is [6C469F301DB5BBD]. We now add [6073] to this, becoming [6C469F301DBBC30]. Remember that a computer adds and multiplies numbers only in binary, so multiplying and adding hexadecimal numbers is very easy for it. Windows’ own calculator application allows multiplication and addition of hexadecimal numbers to be done easily, if you want to do them yourself. We now make the new seed equal to the last 32 bits of this hexadecimal number, or [01DBBC30] (remember that a hexadecimal digit is 4 bits). The random number produced is thus the first 16 bits of this new seed, or [01DB]. Repeatedly invoking the RNG produces the following list of pseudo-random numbers: [01DB], [7B06], [5233], [E470], [5CC4], [36BB], ... It can be shown that the seed variable will become the same as it was at the start of the program only after the RNG is invoked 4,294,967,296 times. In all those RNG invocations, the variable seed would have become equal to every number between 0 and 4,294,967,295 (or between [00000000] and [FFFFFFFF]) exactly once. This essentially means that the random number sequence won’t repeat itself until after that amount of invocations. Pokemon CreationHow the PID of a Pokemon is createdThe game creates a PID from two RNG calls. Since each RNG call results in a 16-bit number, appending these two 16-bit numbers together results in a 32-bit number, which becomes the PID of the Pokemon. The second random number becomes the first 16 bits of the PID, and the first random number becomes the second 16 bits. For example, suppose the two random numbers generated were [01DB] and [7B06] as above. Then the PID of the Pokemon would be [7B0601DB], or 2063991259 in decimal. How to extract information about the Pokemon from its PIDAs was said before, a lot of things about a Pokemon can be known from just its PID. Here, we shall mention only three of these: nature, gender and ability. How to find the nature of a Pokemon from its PIDFirst, convert the PID to decimal as described at the start of the article, and consider only this decimal number’s last two digits. If the number having these two digits is greater than 24, subtract 25 from it, and repeat this procedure until it becomes a number between 0 and 24. This number then corresponds to a particular nature according to the following table:
How to find the gender of a Pokemon from its PIDThis only applies to Pokemon that can be either male or female. If a Pokemon is always genderless (for example Staryu), always male (for example Tauros) or always female (for example Chansey), the Pokemon will, of course, always assume that gender. For the other Pokemon, first take the last two digits of the PID in hexadecimal form and convert that number to decimal. This number should be between 0 and 255. As is commonly known, some Pokemon are more probable to be of one gender than another (for example Bulbasaur). There are four gender categories in all, other than the genderless, always male and always female categories:
How to find the ability of a Pokemon from its PIDThis only applies to Pokemon that can have one of two possible abilities. If a Pokemon can have only one ability, then it will have that ability, of course. For the other Pokemon that can have one of two abilities, first convert the PID to binary, and look at the last bit. If it is 0, the Pokemon will have its first possible ability, while if it is 1, it will have the second possible ability. The following table lists all Pokemon having two possible abilities, showing which ability corresponds to 0 and which corresponds to 1 in Diamond and Pearl (thanks yamipoli for providing this chart):
How the IVs of a Pokemon are createdThe six IVs of the Pokemon are also created from just two RNG calls. Since each IV consists of 5 bits (because the binary number 11111 is equal to 31 in decimal), the first random number would contain 3 of these IVs (5 × 3 = 15), with one redundant bit, while the second random number would contain the other 3. The IVs would be extracted from the two random numbers as follows: First Random Number: x|xxxxx|xxxxx|xxxxx -|DefIV|AtkIV|HP IV Second Random Number: x|xxxxx|xxxxx|xxxxx -|SpDIV|SpAIV|SpeIV For example, given the subsequent two random numbers [5233] and [E470] as above, we would have: First Random Number = [5233] = 0|10100|10001|10011. Hence, the Defense IV would be 10100 = 20, the Attack IV would be 10001 = 17 and the HP IV would be 10011 = 19. Second Random Number = [E470] = 1|11001|00011|10000. Hence, the Special Defense IV would be 11001 = 25, the Special Attack IV would be 00011 = 3 and the Speed IV would be 10000 = 16. Thus, our Pokemon would have the IVs 19/17/20/3/25/16, written in the usual format of HP IV/Atk IV/Def IV/SpA IV/SpD IV/Spe IV. How the RNG is called in the games to generate a PokemonThere are basically four different ways of how the RNG is invoked to produce a Pokemon, depending on the game and the Pokemon:
Methods 2 and 4 are only used in Pokemon Ruby, Sapphire, Emerald, Fire Red and Leaf Green (RSEFRLG) to produce wild Pokemon. All the Pokemon you catch in these games that are not wild Pokemon are created using Method 1. Examples of non-wild Pokemon that you can catch or be given in the game are:
Method 1 is also used for some RSEFRLG wild Pokemon and for all Diamond and Pearl Pokemon, whether they are wild or not. The criterion for choosing whether to use Method 1, 2, or 4 in the creation of wild Pokemon in Ruby, Sapphire, Fire Red and Leaf Green seems to be arbitrary, although it might be related to the terrain where they are situated. To summarise, here are the methods used for each game depending on the Pokemon being caught or given:
A Complete ExampleSuppose you meet a wild Tentacool in Emerald. Let’s assume that Method 2 is chosen for Tentacool to be generated. Also we assume that the current RNG seed is [560B9CE3]. The game calls the RNG and gets the number [2751]. The game calls the RNG again and gets the number [7E48]. Thus, the PID of this Tentacool is [7E482751]. This hexadecimal number is the 32-bit binary number 01111110010010000010011101010001 which is equal to 2118657873 in decimal. The last two digits of this decimal number are 73. Since this number is greater than 24, we subtract 25 from it. 73 minus 25 is equal to 48. 48 is still greater than 24, so we again subtract 25 from it, becoming 23. Hence this Tentacool would have a Careful nature, since that is the nature that the number 23 corresponds to. The last two digits of the PID in hexadecimal are 51, which is equal to 01010001 in binary, or 81 in decimal. Tentacool has a 50% chance of being female. Since the number 81 is between 0 and 126, this Tentacool would be female. The last digit of the binary representation of the PID is 1. Thus, this Tentacool would have the second possible ability, i.e. Liquid Ooze. The game now calls the RNG for a third time, getting the number [CAB4]. This number is discarded since we’re using Method 2 to generate the Pokemon. A fourth call to the RNG yields the number [629C]. This number is equal to 0|11000|10100|11100 in binary. The Defense IV would then be the binary number 11000, which is 24 in decimal, the Attack IV would be 10100, which is 20 in decimal, and the HP IV would be 11100, which is 28 in decimal. A final invocation to the RNG gives us the number [5EE9]. This number is equal to 0|10111|10111|01001 in binary. The Special Defense IV would thus be the binary number 10111, which is 23 in decimal, the Special Attack IV would be 10111, which is 23 in decimal and the Speed IV would be 01001, which is 9 in decimal. To recapitulate, you would have encountered a female Tentacool having a Careful nature, the Liquid Ooze ability and 28/20/24/23/23/9 IVs. More Things affected by the PIDHow the Game Makes a Pokemon ShinyVery rarely, you might encounter a Pokemon having a different coloration than usual. This is called a shiny Pokemon. The game’s criterion for making a shiny Pokemon depends in part on its PID, but also depends on two other numbers. One of these numbers is visible in the game. It is called the Trainer ID. This is the number you have when you look at your Trainer card, and is a number between 0 and 65535 (between [0000] and [FFFF] in hexadecimal). The other number is also a number between 0 and 65535 but is unfortunately invisible to you. It is called the Secret ID. Currently, there’s no way you can know your Secret ID without looking at your save file or without using cheat codes. So how does the game determine that a Pokemon is shiny? Here’s how. Split the PID in two 16-bit numbers. That is, if the PID was [465DB901], it would split it in two as [465D] and [B901]. We shall call the first 4 hexadecimal digits the HID (high ID) and the last 4 hexadecimal digits the LID (Low ID). Now convert the HID, LID, Trainer ID (which we shall denote by TID for short) and Secret ID (denoted by SID for short) to binary, and compare the first 13 bits of all four, like so: HID: xxxxxxxxxxxxx|xxx LID: xxxxxxxxxxxxx|xxx TID: xxxxxxxxxxxxx|xxx SID: xxxxxxxxxxxxx|xxx Here we’re counting the number of 1's in the first bit of the HID, LID, TID and SID. If there are either 1 or 3 ones among those bits, the Pokemon is automatically not shiny. If there aren’t, the game counts the second bits. Again, if there are either 1 or 3 ones among those bits, the Pokemon is automatically not shiny. The game checks the bits of the HID, LID, TID and SID up to their thirteenth one, and all of them would need to have 0, 2 or 4 ones for the Pokemon to be shiny! This is a one in 8192 (2 to the power of 13) chance of happening. Let’s have an example. Suppose the Trainer ID of the player is 43288, or [A918] in hexadecimal, and the Secret ID is 6075, or [17BB] in hexadecimal. Suppose also that the PID of the Pokemon is [B58F0B2A]. We split the PID in two, the HID [B58F] and the LID [0B2A], and convert the HID, LID, Trainer ID and Secret ID to binary. Thus we would have: HID: 1011010110001|111 LID: 0000101100101|010 TID: 1010100100011|000 SID: 0001011110111|011 We now count the number of bits in the first bit, second bit, third bit ... up to the thirteenth bit of each of these four 16-bit numbers. HID: 1011010110001|111 LID: 0000101100101|010 TID: 1010100100011|000 SID: 0001011110111|011 Ones: 2022222420224 Since counting the ones of each bit up to the thirteenth one in turn never yields a number that is not 1 or 3, the Pokemon will be shiny! How to determine whether Wurmple evolves into Silcoon or into CascoonThere are many hoaxes on the internet as to how Wurmple evolves into Silcoon or into Cascoon. Some say that it is determined by gender, other say it is determined by whether or not it is night or day. The truth is that it is determined from the PID of Wurmple as follows. Look at the last 16 bits of the PID. We have called it the LID in the previous section. Convert the LID to decimal, and look at the last digit of this decimal number. If this digit is 0, 1, 2, 3 or 4, Wurmple would evolve into Silcoon. If the digit is 5, 6, 7, 8, or 9, Wurmple would evolve into Cascoon. Let’s provide an example. Say a Wurmple has a PID of [5CF4091C]. The last 16 bits of the PID are [091C], which is equal to 2332 in decimal. Since the last digit of this decimal number is 2, this Wumple would evolve into Silcoon. How to determine Unown’s shapeAs claimed previously, even Unown’s shape is determined from its PID. First convert the PID to binary. Look at the seventh, eighth, fifteenth, sixteenth, twenty-third, twenty-fourth, thirty-first and thirty-second bits of the PID, i.e. the ones underlined below: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Combine these 8 bits together to form an 8-bit number, and convert it to decimal. If this number is 28 or more, subtract 28 from this number until it is a number between 0 and 27. This number would then correspond to the Unown symbol according to the following table:
Let’s give an example. Suppose an Unown has PID [4C07DE71]. We first convert this PID to binary, getting 01001100000001111101111001110001 Combine the underlined bits together, getting 00111001, and convert it to decimal, getting 57. 57 is larger than 27, so it is subtracted by 28, becoming 29. 29 is again larger than 27, so it is subtracted by 28, getting 1. 1 corresponds to B, and hence the Unown would have the shape of a B. How Spinda’s spots are determinedSpinda’s spots are the most useless thing ever implemented in the history of computer implementations, but, for the sake of completeness, we explain exactly how Spinda’s spots are placed on its rubbery body, because the PID, again, is the culprit. Look at Spinda’s PID in hexadecimal. Convert each hexadecimal digit into decimal in turn. So, for example, if Spinda’s PID is [65492BDA], we would have 6 = 6, 5 = 5, 4 = 4, 9 = 9, 2 = 2, B = 11, D = 13 and A = 10. From these numbers, the co-ordinates (5, 6), (9, 4), (11, 2) and (10, 13) are thus constructed. These correspond to the coordinates of the upper-left hand corner of Spinda’s left face spot, its right face spot, its left ear spot and its right ear spot respectively. (Spinda’s left spots appear on the right in its front sprite and vice-versa.) All Spinda have exactly 4 spots. If some Spinda appear to have less than four spots, it is either because some spots are superimposed on each other or because the coordinate for some spots does not fall completely on Spinda’s sprite. To put them in place, the game adds 18 to the x-coordinate and 19 to the y-coordinate of the left face spot. It also adds 6 to the x-coordinate and 18 to the y-coordinate of the right face spot. Lastly, it adds 24 to the x-coordinate and 1 to the y-coordinate of of the left ear spot. The right ear spot’s coordinates are left unchanged. Thus we have: Left face spot upper-left hand coordinates: (23, 25) Right face spot upper-left hand coordinates: (15, 22) Left ear spot upper-left hand coordinates: (35, 3) Right ear spot upper-left hand coordinates: (10, 13) The PID of Chained Shiny PokemonA new feature in Diamond and Pearl is the PokeRadar, which allows you to chain Pokemon. Chained Pokemon, besides having the advantage of always catching Pokemon of the same species, have a greater probability for them to appear shiny. This section explains in detail how the game determines whether or not a chained Pokemon is shiny, and how it constructs its PID so that it conforms to the criterion that appears in the previous section entitled ‘How the game makes a Pokemon shiny’. The Probability of a Chained Pokemon to appear ShinyThis depends on the chain number you’ve attained, which cannot be greater than 40. Suppose your chain number is c. The probability that a chained Pokemon is shiny is governed by the following formula: Probability = floor((14747 – 40 × c) ÷ (1640 – 40 × c)) ÷ 65535 where floor(x) is x rounded down. Hereunder is the probability of catching a shiny Pokemon knowing your chain length:
Notice that the probabilities are not very good unless your chain’s length becomes 30 or more, so aim for as long a chain as possible. How the PID and IVs of a Chained Shiny Pokemon is createdFirst, the game calls the RNG twice to create the PID as usual. Remember that the first number generated would be the LID (last 16 bits) of the PID, while the second one would be the HID (first 16 bits) of the PID. The game now needs to ‘fix’ the PID such that the criterion for the Pokemon to appear shiny is attained. This is done as follows. Convert the Trainer ID and Secret ID to binary, and look at their thirtheenth, twelfth, eleventh, etc. bits of both, until we reach their first bit. Suppose we’re looking at the i’th bit. For each of these pairs of bits, the RNG is called once and the resulting random number’s last bit is noted. If the pair of bits at the i’th position of TID and SID are the same (i.e. either both are 0 or both are 1), the HID and LID’s i’th bits are both set to the last bit of the random number generated. If the pair of bits at the i’th position of TID and SID are different (i.e. one is 0 and the other one is 1), the LID’s i’th bit is set to the last bit of the random number generated, and the HID’s i’th bit is set to the reverse bit (1 if it was 0, 0 if it was 1) of the last bit of the random number generated. Following this procedure, this would ensure that the number of 1’s in the i’th position of the TID, SID, HID and LID is either 0, 2 or 4. Since this would be true for all bits between the first and the thirteenth, the Pokemon would be ‘forced’ to appear shiny. After this is done, there are two subsequent calls to the RNG to create the IVs as usual. Thus, the RNG is called 17 times in all to generate a chained shiny Pokemon: twice to generate the initial PID, 13 times to ‘fix’ the PID, and twice to generate the IVs. An Example of generating the PID of a Chained Shiny PokemonSuppose the player encounters a chained shiny Pokemon. We suppose the player has Trainer ID (TID) 38710 and Secret ID (SID) 13099. We also suppose that the current RNG seed is [69AC550F]. We call the RNG twice, getting the numbers [18E5] and [1DD6]. Hence, the LID is [18E5] and the HID is [1DD6]. The numbers in binary are: TID: 1001011100110110 SID: 0011001100101011 HID: 0001110111010110 LID: 0001100011100101 We now need to fix the HID and LID such that the Pokemon is shiny. We start looking from the thirteenth bit. We call the RNG, getting [A013]. When converted to binary, its last digit is 1. (To know at a glance if the last bit of a hexadecimal number is 0 or 1, look at the last hexadecimal digit. If it is 0, 2, 4, 6, 8, A, C or E, the last bit would be 0, otherwise it would be 1.) The TID and SID’s thirteenth bits are 0 and 1 respectively, hence they are different. Thus, we make the thirteenth bit of LID as 1 (last digit of the last random number generated) and the first bit of HID as 0. So we have: TID: 1001011100110110 SID: 0011001100101011 HID: 0001110111010110 LID: 0001100011101101 Next we go to the twelfth bit. We generate another random number, getting [A4AE], whose last bit is 0. TID’s and SID’s twelfth bits are 1 and 0 respectively. Hence the twelfth bit of the LID is made to be 0 and the HID's twelfth bit is made to be 1. TID: 1001011100110110 SID: 0011001100101011 HID: 0001110111110110 LID: 0001100011101101 Moving on to the eleventh bit, we call the RNG again, getting [77AD] whose last bit is 1. The TID and SID’s eleventh bit are both 1. Thus we make both the HID and the LID's eleventh bits to be 1. TID: 1001011100110110 SID: 0011001100101011 HID: 0001110111110110 LID: 0001100011101101 For the tenth bit, we again call the RNG, getting [B237] whose last bit is 1. The TID and SID’s tenth bits are both 0. Thus we make the fourth bit of LID and HID both 1. TID: 1001011100110110 SID: 0011001100101011 HID: 0001110111010110 LID: 0001100011101101 Continuing doing this until we reach the first bit, we get: TID: 1001011100110110 SID: 0011001100101011 HID: 1100001101110110 LID: 0110011101101101 Ones: 2222024402422 The last bit of each of the subsequent random numbers generated was 0, 1, 1, 1, 0, 0, 1, 1, 0 for the ninth, eighth, seventh, ... first bit respectively. As you can see, the HID and LID now satisfy the criterion for the Pokemon to appear shiny. Thus the PID of the chained shiny Pokemon would be 11000011011101100110011101101101 which is equivalent to [C376676D] in hexadecimal, or 3279316845 in decimal. Two subsequent RNG calls would then be made to create the Pokemon's IVs normally. |