# Anydice formula to calculate hit percentage against AC 5-25

I'm trying to create what I think is a simple formula in Anydice and struggling to figure it out.

I roll a 20-sided dice, add a number x, and want to know the odds that the number will be greater or equal to a number between 5 and 25. For those who play D&D, basically:

If a fighter has a +8 to hit, what is the percentage chance that he'll hit AC 5-25?

I think this is it, but he never fully shows the entire formula, and when I copy it into anydice, it keeps returning an error: https://anydice.com/articles/dnd4-attacks/

• Welcome to RPG.SE! Take the tour if you haven't already, and check out the help center for more guidance. – V2Blast Feb 2 '20 at 3:50

# Here, use this

The following anydice program calculates the chance to hit with a given bonus against a given AC. Note that in it crits are treated the same as normal hits and that you'll want to view the data transposed.

function: attack D:n plus BONUS:n vs AC:n  {
if D = 20 { result: 1 }
else if D = 1 { result: 0}
else if D + BONUS >= AC { result: 1 }
else { result:0}
}

loop AC over {5..25} {
output [attack d20 plus 8 vs AC] named "[AC]"
}


To explain the function: it takes in three variables — our d20, our bonus, and AC — and casts them to numbers. This means the function is run for each value on the die. We then go through the scenarios, testing for critical hit, critical miss, normal hit, and otherwise miss. We return 1 for hits and 0 for misses.

To get out output we run it for each AC giving an output for each. Each output has two results (0 and 1) with different probabilities. Transposing the data gives two graphs over AC, the hit and miss probabilities.