AnyDice

middle NUMBER of DICE

This function yields the sum of the middle NUMBER values rolled by DICE. Dice positions are rounded down, so the middle of four dice is the second die.

Examples

output [middle 1 of 3d6] named "2nd die"
output [middle 2 of 3d6] named "1st and 2nd die"
output [middle 1 of 4d6] named "2nd die"
output [middle 2 of 4d4] named "2nd and 3rd die"
output [middle 3 of 4d6] named "1st, 2nd and 3rd die"

Do it yourself

function: middle NUMBER:n of DICE:d {
 if NUMBER = #DICE { result: DICE }

 if NUMBER = 1 { result: (1 + (#DICE - 1) / 2) @ DICE }
 
 FROM: 1 + (#DICE - NUMBER) / 2
 TO: FROM + NUMBER - 1
 result: {FROM..TO}@DICE
}