# AnyDice: Second highest roll from mixed pool

I'm struggling to adapt Ilmari Karonen's answers from these two questions to my problem.

I want to look at the second highest value from various pools.

function: max A:n B:n C:n {
result: 2@[sort {A, B, C}]
}
output [max 1d12 1d10 1d8]


Produces possibly believable results, but:

output [max 2d12 0d10 0d8]


Is not at all believable, and completely disagrees with:

output 2@2d12


Similarly testing with

function: maxs A:n {
result: 2@[sort {A}]
}
output [maxs 2d12]


hasn't helped.

You were pretty close — all you needed was to replace the :n type specifiers with :s, so that your dice rolls will be passed to the function as sequences, instead of being summed into single numbers, like this:

function: second highest of A:s B:s C:s {
result: 2@[sort {A, B, C}]
}
output [second highest of 1d12 1d10 1d8]


In fact, you might as well generalize this function into:

function: P:s at A:s B:s C:s {
result: P@[sort {A, B, C}]
}
output [2 at 1d12 1d10 1d8]


which works just like the built-in @ operator, except that it can take multiple dice pools on the right hand side and merge them before selecting (and summing) one or more positions.

Just played around with it (never used AnyDice before) and it looks like the problem is with what you think is in your groups.

function: max A:n B:n C:n {
result: 2@[sort {A, B, C}]
}
output [max 2d12 0d10 0d8]


Gives us a set A = d12 + d12 (range of 2 to 24) but we're sorting the 3 sets (A,B,C) not the individual dice so in first position is d12+d12 but second position would always be 0 because you've rolled B or C 0 times.

If you use:

function: max A:n B:n C:n {
result: 2@[sort {A, B, C}]
}
output [max 1d12 1d12 0d8]


You get the right answer because set B is now a d12 too.