So for a roll20 D&D game I been working on I decided it'd be nice to have a macro that would calculate the probability of successfully hitting an enemy and showing it as a percentage.
To hit an enemy you must roll higher on your accuracy than their evasion, and both of these rolls are D20s. However, both accuracy and evasion are increased/decreased by various attributes and proficiencies.
From what I remember probability is the winning possibilities / total possibilities.
What I'm having problems with though is figuring out what the winning possibilities are when the accuracy/evasion bonuses/debuffs are applied.
I know that using triangular numbers you could calculate the chances of rolling a number equal to or higher than a 2nd dice roll. However, I do not know what math formula to use to calculate the modifiers.
For example: This is the possibilities for 2d6 with no modifiers \begin{array}{c|cccccc} &1&2&3&4&5&6 \\ \hline \\ 1&=&<&<&<&<&< \\ 2&>&=&<&<&<&< \\ 3&>&>&=&<&<&< \\ 4&>&>&>&=&<&< \\ 5&>&>&>&>&=&< \\ 6&>&>&>&>&>&= \\ \end{array} However, if you had a +1 added to your rolls then the possibilities for 2d6 would look like this: \begin{array}{c|cccccc} &1&2&3&4&5&6 \\ \hline \\ 1&<&<&<&<&<&< \\ 2&=&<&<&<&<&< \\ 3&>&=&<&<&<&< \\ 4&>&>&=&<&<&< \\ 5&>&>&>&=&<&< \\ 6&>&>&>&>&=&< \\ \end{array}
So my problem is I can't figure out the formula used to calculate the increase each time. No mod 2d6 is 21 wins, but +1 would by 26 (+5), +2 would be 30 (+9) and +3 would be 33 (+12)
I apologise if this has been asked before in some way and I haven't found it. My internet has been pretty bad today so the amount of searching I done was limited.
Assuming no bonus, the probability of a successful hit (assuming ties go in favor of the attacker) would be $\frac{1}{20}+\frac{1}{2}(1-\frac{1}{20})=\frac{21}{40}$, seen easily by noting the probability that the two roll exactly the same is $\frac{1}{20}$ and the remaining cases split evenly between favoring the attacker and favoring the defender.
For a net difference of $1$ in favor of the attacker (E.g. by having +1 to attack rolls and +0 to defense, or +5 to attack and +4 to defense, etc...) we would include an additional $19$ favorable scenarios for the attacker out of the $400$ possible outcomes of rolling two dice giving a probability of $\frac{229}{400}$
A net difference of $2$ in favor of the attacker would give an additional $18$ more favorable scenarios beyond those mentioned in the previous paragraph giving a probability of $\frac{247}{400}$, so on and so forth.
In general: for a net difference of $n\geq 0$ in favor of the attacker, the probability would be $\dfrac{-n^2+39n+420}{800}$
This was found by recurrence relations, letting $f(0)=\frac{21}{40}$ and $f(n)=f(n-1)+\frac{20-n}{400}$ by generalizing the observations made above.
A similar technique can be applied in the reverse direction to get the probabilities for if the defender has the advantage, or you can just use the values already found for the attacker having the advantage and subtracting away from $1$. The probability of a successful attack with a net difference of $n\geq 1$ in favor of the defender would correspond to $1$ minus the corresponding probability of a successful attack with net difference of $n-1$ in favor of the attacker.