# Anydice probabilities that allow for addition or subtraction of one die from another

I'm new to anydice, so forgive me if I've overlooked something obvious, but I can't seem to figure this one out.

I'm working on a dice mechanic that allows the player to choose whether or not they add or subtract their base die roll to/from another die roll.

I can do d10 + d6, I can do d10 - d6, how do I do d10 +|- d6?

Here's a generic AnyDice function for evenly mixing two different possible dice rolls:

function: mix A:d and B:d weighted by X:n {
if X = 1 { result: A } else { result: B }
}
function: mix A:d and B:d {
result: [mix A and B weighted by d2]
}


For example, the expression [mix 1d10 + 1d6 and 1d10 - 1d6] will return the distribution of outcomes obtained by rolling a d10 and a d6, and randomly choosing whether to add or subtract the d6 from the d10. The resulting probability distribution, plotted as a line graph, will look like this:

Of course, in your question you say that the player gets to choose whether to add or subtract the d6, and they probably won't be doing so at random. Since you didn't tell us what the player is trying to achieve with the roll, it's hard to give any specific answer, but a general technique for modeling such player choice mechanics in AnyDice is to write a function that takes the possible outcomes of the two rolls (i.e. two number parameters, tagged with :n), and returns the one more favorable to the player (whichever that might be).

For a random example, if the player is trying to roll as close to 10 as possible, you could write your decision function like this:

function: better of A:n and B:n {
if [absolute A - 10] < [absolute B - 10] { result: A }
else { result: B }
}


However, note that there's a catch: if you call this function as [better of 1d10 + 1d6 and 1d10 - 1d6], then you're actually modeling a mechanic where the player first rolls 1d10 + 1d6, and then separately rolls 1d10 − 1d6, and chooses the better of those two independent rolls. If you instead want the player to roll a single d10 and a single d6, and then choose whether to add or subtract them, then you need a helper function to "freeze" the d10 and d6 rolls before adding and subtracting them, like this:

function: better of A:n plus minus B:n {
result: [better of A+B and A-B]
}


The resulting probability distributions, for various target numbers, will then look like this:

Actually analyzing the gameplay effects of all those funky-looking probability distributions is left as an exercise for the reader. ;)

Ps. The [better of A and B] function I gave above is slightly biased, in that it will always choose the second option if both are equally good (i.e. the same distance from 10). You can make the bias go the other way just by changing the < into <=, but if you want to completely eliminate it (by breaking ties at random), you can rewrite the function like this:

function: better of A:n and B:n {
if [absolute A - 10] < [absolute B - 10] { result: A }
if [absolute A - 10] > [absolute B - 10] { result: B }
result: 1d{A, B}
}


This code first checks if either roll is strictly better than the other, and if so, chooses that. If the rolls A and B are equally good, it instead returns a custom die that will roll either A or B with equal probability, simulating a random choice. (Of course, if you have some other criteria for breaking ties, you can insert additional if statements before the last line of the function above, or even replace the last line entirely if you know that it will never run.)

A bit surprisingly, making that change to the example code above changes the appearance of the graphs quite a bit:

Actually, with that particular example, it's perhaps not all that surprising: with a target number between 1 and 10, you have a 10% chance of rolling exactly that number on the d10, at which point (if you're just aiming as close as possible to the target, like the example assumes) it makes no difference whether you add or subtract the d6 roll. Thus, breaking such ties randomly, instead of always preferring subtraction, increases 5% of all rolls by an average of 7 points (twice the average roll of the d6). Sure enough, setting the target number to 0 or 11 causes the tie-breaking rule not to matter.

Of course, if it really makes no difference in your actual mechanics whether the player rolls N points below or N points above the target, then you'd really want to plot you results in such a way as to group those outcomes together. One possibility would be to actually plot the distance of the player's roll to the target, like this:

output [absolute 10 - [better of 1d10 plus minus 1d6]]


This change makes the output a lot more readable (IMO, at least):

It also highlights the symmetry of this example mechanic: it's equally easy to aim for 1 as it is to aim for 10 with a roll of 1d10 ± 1d6, and thus the graphs for those two target values overlap exactly in the plot above.

• This is a spectacular answer. You've answered every aspect of my question, including some I didn't know I had. Thank you for the work you've put in. – Michael Conn Mar 3 '17 at 16:40
• No problem, I rather like these kinds of questions; AnyDice, even with all its quirks, is a fun toy to play with. BTW, I expanded my answer a little bit, noting that (at least for the example mechanic I used) tie-breaking can make a noticeable difference, as can the way the results are presented. – Ilmari Karonen Mar 4 '17 at 12:30

The formula that will achieve what I think is your desired output:

output 1d10 + d{-6,-5,-4,-3,-2,-1,1,2,3,4,5,6}

This will show you the range of odds for any value -6 to +6 added to a d10 roll.

Based on the feedback received from Michael in the comments, I am adding this here as well to make my answer more complete:

An anydice script that has two outputs:

output 1d10 + 1d6 output 1d10 - 1d6

Individually these will give the odds of any value coming up for that specific output. When the data are transposed, however, it will transpose the data across the results rather than across the outputs, which will show the relative probabilities for each result for each output.

• @doppelgreener, Right! Sorry, I was rolling 3d10 for some of my figures. So here's a proper example. Again, I really appreciate the help! While that will give the proper range, I'm pretty sure it skews the probabilities. For example: d10 + d6 gives me a 1.67% chance of getting an 2, d10 - d6 gives me a 10% chance of getting an 2. Given the mechanic, I've got an 11.67% chance of getting a 2 on any roll. The output you suppied shows a 5.83% chance for it since I'm essentially rolling a custom d12 instead of a d6. – Michael Conn Feb 27 '17 at 20:09
• @MichaelConn Actually, it's right on the money. The odds of getting a 2 with a +d6 are 1.67%, the odds with the -d6 is 10%. The average of those two values is, in fact, 5.835% which is right where it falls with the +/- output. – LegendaryDude Feb 27 '17 at 20:21
• @MichaelConn 11.67% divided by two (we've doubled the size of the pool of possibilities) is 5.83%. Also, that custom d12 represents +-d6, which has twelve equally likely possibilities, each one represented on that d12. – doppelgreener Feb 27 '17 at 20:22
• @MichaelConn There are actually 120 combinations since you have the 60 from +d6 and 60 from -d6. So you have 1/60 for +d6 and 6/60 for -d6 – diego Feb 27 '17 at 21:29
• @MichaelConn Two separate output statements: output 1d10 + 1d6 and output 1d10 - 1d6, then for data type select 'transposed.' Ironically, this was going to be my first answer before I decided it probably wasn't what you're looking for. It won't add the results but it will transpose the data across the results rather than across the outputs, which will allow you to analyze in the way you're looking to. – LegendaryDude Feb 27 '17 at 22:00