Den generation occurs at sub_7100E91740. The RNG for den/raid generation is xoroshiro128+.
Like egg generation it uses nn::crypto::GenerateCryptographicallyRandomBytes which makes the den type unable to be RNGed. This function is responsible for setting the den seed, star count, species, rarity, etc.
Raid generation occurs at sub_710076FAD0. An important note is that the call to the RNG is a slightly modified version of xoroshiro128+. A TLDR is that it will modulo the result by the next power of two until the result is less then the desired max value. It is as follows:
nextState(): # Refer to the link above to see how xoroshiro128+ generates the next state and rand value nextPowerTwo(num): if num & (num - 1) == 0: return num - 1 result = 1 while result < num: result <<= 1 return result - 1 next(maxValue): mask = nextPowerTwo(maxValue) result = nextState() & mask while result >= maxValue: result = nextState() & mask return result
Step 1. EC
The encryption constant is set a value of next(0xffffffff).
ec = next(0xffffffff)
Step 2. TID/SID
A temporary TID/SID is set to a value of next(0xffffffff).
otid = next(0xffffffff)
Step 3. PID
The PID is set a value of next(0xffffffff).
pid = next(0xffffffff)
Step 4. Shiny
Shininess for the raid is determined by the temporary TID/SID. Later on any PID modifications will be done using the real TID/SID
if shinyType == 0: # Random Shiny Chance otsv = ((otid >> 16) ^ (otid & 0xffff)) >> 4 psv = ((pid >> 16) ^ (pid & 0xffff)) >> 4 if otsv == psv: # Shiny shiny = True if (otid >> 16) ^ (otid & 0xffff) ^ (pid >> 16) ^ (pid & 0xffff): shinyType = 2 else: shinyType = 1 if psv != realTSV: # Force PID to be shiny from the real TID/SID high = (pid & 0xffff) ^ realTID ^ realSID ^ (shinyType == 1) pid = (high << 16) | (pid & 0xffff) else: # Not shiny shiny = False if psv == realTSV: # Force PID to be not shiny from the real TID/SID pid ^= 0x10000000 elif shinyType == 1: # Force non-shiny shiny = True psv = ((pid >> 16) ^ (pid & 0xffff)) >> 3 if psv == realTSV: pid ^= 0x100000000 else: # Force Shiny if ((pid >> 16) ^ (pid & 0xffff) ^ realTID ^ realSID) >= 16: high = (pid & 0xffff) ^ realTID ^ realSID pid = (high << 16) | (pid & 0xffff)
Step 5. IVs
The amount of 31 IVs that there will be is dependent on the star count of the den type using next(6) calls. After the 31 IVs are assigned the rest of the IVs are filled using next(32)
ivs = [-1]*6 i = 0 while i < ivCount: stat = next(6) if ivs[stat] == -1: ivs[stat] = 31 i += 1 for i in range(6): if ivs[i] == -1: ivs[i] = next(32)
Step 6. Ability
There are two ability groupings a raid can have. A raid can either pull from Ability0/Ability1 (next(2)) or Ability0/Ability1/Ability2 (next(3)).
if abilityType == 4: # Allow hidden ability ability = next(3) elif abilityType == 3: # Don't allow hidden ability ability = next(2) else: # Locked ability ability = abilityType
Step 7. Gender
There are 4 different gender types a raid can have. It can either be random, female, male, or genderless. If it's random it uses a rand(253) call and compares to the gender ratio for female/male.
if genderType == 0: # Random gender if genderRatio == 255: # Locked genderless gender = 2 elif genderRatio == 254: # Locked female gender = 1 elif genderRatio == 0: # Locked male gender = 0 else: # Random gender gender = next(253) + 1 < genderRatio elif genderType == 1: # Male gender = 0 elif genderType == 2: # Female gender = 1 elif genderType == 3: # Genderless gender = 2
Step 8. Nature
Nature is set to a value of next(25). If the raid pokemon is Toxtricity then there is some special handling.
if species != Toxtricity: nature = next(25) else: if form == Amped: natures = [ 3, 4, 2, 8, 9, 19, 22, 11, 13, 14, 0, 6, 24 ] nature = natures[next(13)] else: # Lowkey natures = [ 1, 5, 7, 10, 12, 15, 16, 17, 18, 20, 21, 23 ] nature = natures[next(12)]
Step 9. Height/Weight
Height and weight are set to a value of next(129) + next(128).
height = next(129) + next(128) weight = next(129) + next(128)