5
\$\begingroup\$

How do I calculate the following in AnyDice?

2d6, subtract lowest result from highest.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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! \$\endgroup\$
    – Someone_Evil
    Jul 15, 2019 at 11:43

3 Answers 3

13
\$\begingroup\$

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

output [absolute d6 - d6]
\$\endgroup\$
6
  • \$\begingroup\$ This is a correct approach but could you briefly note why the method works? Maybe a small example \$\endgroup\$
    – Sdjz
    Jul 15, 2019 at 11:52
  • 1
    \$\begingroup\$ Ow, that was simple. Thanks! \$\endgroup\$ Jul 15, 2019 at 11:53
  • \$\begingroup\$ What if the roll is "3d6, subtract lowest one" ? \$\endgroup\$
    – enkryptor
    Jul 15, 2019 at 12:09
  • 1
    \$\begingroup\$ @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]. \$\endgroup\$
    – Carcer
    Jul 15, 2019 at 12:52
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Blckknght
    Jul 15, 2019 at 13:07
7
\$\begingroup\$

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:

result graph

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thank you. Blckknght's was indeed a simpler code, but yours is "even more correct". \$\endgroup\$ Jul 15, 2019 at 11:56
4
\$\begingroup\$

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.)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .