# How can I calculate the difference between two rolls in AnyDice?

How do I calculate the following in AnyDice?

2d6, subtract lowest result from highest.

• Welcome to RPG.SE! Take the tour if you haven't already, and check out the help center or ask us here in the comments (use @ to ping someone) if you need more guidance. Good Luck and Happy Gaming! – Someone_Evil Jul 15 '19 at 11:43

I suggest subtracting without regard to which die in higher, then taking the absolute value:

output [absolute d6 - d6]

• This is a correct approach but could you briefly note why the method works? Maybe a small example – Sdjz Jul 15 '19 at 11:52
• Ow, that was simple. Thanks! – Roelosaurus Jul 15 '19 at 11:53
• What if the roll is "3d6, subtract lowest one" ? – enkryptor Jul 15 '19 at 12:09
• @enkryptor a trivial function for "of XdY, subtract the lowest from the highest" is function: subtract DICE:s { result: 1@DICE - #DICE@DICE }, invoked for example: output [subtract 3d6]. – Carcer Jul 15 '19 at 12:52
• @Carcer: I'm not sure that's exactly what enkryptor wanted. If they want to subtract the smallest die from the sum of all the others, then they want function: subtract DICE:s { result: {1..#DICE-1}@DICE - #DICE@DICE } or more concisely function: subtract DICE:s { result: DICE - 2* #DICE@DICE } like this. – Blckknght Jul 15 '19 at 13:07

You can use the following anydice code to do this:

function: X:n odiff Y:n  {
if X > Y {
result: X-Y
}
else {
result: Y-X
}
}
output [d6 odiff d6] named "difference between 2d6"


This checks which die is higher then subtracts accordingly, providing these results:

Note that after doing this and comparing it to Blckknght's answer the results are the same but the other answer has a simpler code so I will just leave this here as a bit of a learning exercise.

• Thank you. Blckknght's was indeed a simpler code, but yours is "even more correct". – Roelosaurus Jul 15 '19 at 11:56

The general way to do this in AnyDice is to write a function that takes a sequence as a parameter, and pass the dice roll into the function, e.g. like this:

function: highest minus lowest of ROLL:s {
result: 1@ROLL - #ROLL@ROLL
}

output [highest minus lowest of 2d6]
output [highest minus lowest of 3d6]


The key element here is the :s after the parameter name. That's what tells AnyDice that the ROLL parameter should be a sequence of numbers, rather than a single number or a (pool of) di(c)e.

What AnyDice actually does, when you pass a pool of dice into a function expecting a sequence like this, is that it executes the function for each possible roll of the dice, assigning the rolled numbers to the sequence (sorted from highest to lowest by default). It then collects the results returned by the function into a single biased die, whose possible outcomes are weighted according to the probability of obtaining each of those results from the dice roll.

Thus, inside the function, the dice are effectively "frozen" into a sequence of fixed numbers, and you can do any math or other manipulation on those numbers that you want.

This is actually a general trick for doing arbitrary calculations on the results of a dice roll in AnyDice. While very useful, it does have two notable drawbacks:

1. It can be slow for large numbers of dice (and/or large numbers of sides per die), because it calls the function for each possible outcome of the roll. In particular, if you try to calculate something like [highest minus lowest of 100d6], it will almost certainly time out, because AnyDice isn't smart enough to realize that only the highest and the lowest number matter, and will instead try to iterate by brute force over all the possible sequences of numbers one could roll with 100d6 (of which there are a bit over 79 million).

2. Since AnyDice collects the results of the function into a biased die, and since it doesn't support sequence-valued dice, you can't usefully return a sequence from a function called like this. If you try, it just gets summed into a single number. (You can, however, return a die from such a function, and it will behave exactly as you would expect.)