I'm trying to create a little tool for comparing dice roll probabilities (for table top gaming). I've got a way of calculating "Roll $n$D$s$ pick highest" and "Roll $n$D$s$ and sum" (where $n$ is the number of dice and $s$ is the number of sides on the dice), but I haven't been able to work out how to do "Roll $n$D$s$ and add the highest and lowest".
This answer shows the probability formula for the Nth ordered discrete random variable having a given value.
I assumed that I could get from there to "Roll $n$D$s$ and add the highest and lowest = T" by summing the probabilities of each way to achieve that score (e.g. 5 from $T_1=1,T_n=4$ and $T_1=2,T_n=3$†), where the probability for a pair is the probability of both the case $k=1$ being the lower score and the case $k=n$ being the higher score (i.e. $Pr(X_{(1)} = T_1) \cap Pr(X_{(n)} = T_2)$).
But that isn't working. My best guess is because results are actually related rather than independent, and so $Pr(X_{(n)} = 4)$ includes times when $Pr(X_{(1)} > 1)$ and $Pr(X_{(1)} = 1)$ includes cases where $Pr(X_{(n)} \neq 4)$, and so it's not as simple as ANDing the probabilities of the individual cases.
So, how can I calculate "Probability of rolling $X$ on $n$D$s$ when adding the highest and lowest", or at least $Pr(X_{(1)} = T_1 \cap X_{(n)} = T_2)$ (i.e. both conditions hold on the same roll)?
(A continual WIP JavaScript implementation is currently at https://ibboard.co.uk/dice-roll-compare/temp.html but could be in any state when you read this!)
† or a special case for when $T_1 = T_2$, because there's only one way to roll that on any number of dice! Although the special case might not be necessary if I fix the calculations.
As you write, the case $T_1=T_2$ is easy, since there's exactly one roll that achieves it. So assume $T_2\gt T_1$. Then the probability that all rolls lie in $[T_1,T_2]$ is
$$ \left(\frac{T_2-T_1+1}s\right)^n\;. $$
But this also includes events where either $T_1$ or $T_2$ isn't actually rolled, so we have to subtract both of those cases:
$$ \left(\frac{T_2-T_1+1}s\right)^n-2\left(\frac{T_2-T_1}s\right)^n\;. $$
But now the cases where neither $T_1$, nor $T_2$ was actually rolled have been subtracted twice, so we have to add them back in once:
$$ \mathsf P(X_{(1)}=T_1\land X_{(n)}=T_2)=\left(\frac{T_2-T_1+1}s\right)^n-2\left(\frac{T_2-T_1}s\right)^n+\left(\frac{T_2-T_1-1}s\right)^n\;. $$