it is based entirely on random chance, spawn rates are determined by the RNG ~
for example a shiny is 1/8172 (roughly, idr the exact number but it's along those lines) but even if you went through 8172 of the same pokemon you aren't guaranteed a shiny, but it also works that you can find a shiny after going through 999999999 pokemon, & by chance find another shiny directly after. the same applies to all pokemon & their chance percentage of spawning.