I'm just going to go ahead and answer this with the actual answer. It is random. There is no specific formula.
Naturally as everyone knows all RNG or Random Number Genration is used to create random events in videogames. In terms of Pokemon, it decides all kinds of things like when you land critcal hits or miss. In this case, what type your friend safari will be. The formula states the randomness, but there isn't one specific for the result. This specific generator isn't a true RNG, but rather uses a formula that makes the generation's results appear equally as often; appearing to be random. For a more detailed explanation, check out this Wikipedia article about RNG.
It is a long ass code that I am not going to spend time typing out, so yeah. There you go everyone. My source is myself, I am a hacker and have multiple users who can confirm this.