I finally figured out how wild encounters are determined. It is also the first time I try to read any game code:
First of all, the game will check the number of steps you have taken on grass in that area (or steps in a cave). That number is reseted when you change to anotehr area, and when you have an encounter, but NOT when you battle a trainer. If the number of steps is less than 5 (on grass) or less than 7 (on a cave), then the RNG is called, if RNG value divided by 656 is higher or equal than 5, there will be no encounter, else RNG will be called again.
If the number of steps is higher (than 5 on grass, or than 7 on a cave), the first RNG call is skipped.
The number of steps is stored in 0x0228FCB8
On the second RNG call, if RNG divided by 656 is higher or equal than 40 (running or walking) or 70 (biking), then there will be no encounter. Else RNG is called another time, and if RNG divided by 656 is higher or equal than 30 (for grass) or than 10 (for cave), there will be no encounter. If it's lower, there will be an encounter.
The value that is terrain dependant is stored in 0x02291834.
If water, the value is stored in 0x02291900
Just in case the explanation above is hard to understand, I'll write how the formula would look in C code:
Language: c
int terrain_value = readbyte(0x02291834); //On normal grass, terrain_value = 30, on cave, terrain_value = 10.
int counter = readbyte(0x0228FCB8);
int max_counter = 8 - ((terrain_value * 256) / 10) / 256; //for grass, max_counter = 5, for cave, max_counter = 7
if (max_counter > counter){
counter++;
if (rand() / 656 >= 5) return false;
}
if (rand() / 656 >= value) return false; // value = 40 when walking or running, value being 70 when biking.
if (rand() / 656 >= terrain_value) return false;
else return true;
This means that for each grass step, you have a 12% rate of finding encounters, and 0.6% in the first 5 steps, and on a cave, you have a 4% to find an encounter, and 0.2% on the first 7 steps.
If you're biking, then rates are 21% (1.05% on first steps) for grass, and 7% (0.35%) for caves