# Roll Xd6, keeping highest value and adding +1 for each duplicate of that value?

I'm making a card/dice game and I'm trying to set something up on Anydice to compare the odds of the dice rolls, but I'm having some trouble wrapping my mind around some of the language Anydice uses.

What would I need in order to roll a set of dice, keep the highest value, and add +1 for each copy of that die?

For instance, if I rolled [3, 5, 5], I'd end up with a result of 6 (5 + 1), and [2, 2, 5, 5, 5] would have a result of 7 (5 + 1 + 1).

I don't care if there ends up being a higher result with lesser dice (for instance, rolling [5, 5, 5, 6] could have a result of 7, but should have a final result of 6), but that's mostly for the sake of simplicity, as the odds of rolling enough lower duplicates to outperform a roll of higher value is low enough to ignore.

This is the BG.SE question where the dice system's requirements originally came from (and why it's relevant).

• Are solutions to the statistics of this problem that don’t involve anydice acceptable? Apr 14, 2021 at 1:13
• This is a very curious mechanic. It looks interesting on the surface, but it has an overwhelming probability of just rolling 6 for any number of dice between 2 and 11. From 12 dice onwards, 7 now outpaces 6, and only from 18 dice onwards, do you get a probability of rolling 8 that's higher than rolling a 7. May I ask what you intend to use this for? Apr 14, 2021 at 8:58
• An interesting feature of this mechanic is that it makes rolling a 1 impossible if there's more than one die in the pool. In that case, the worst possible roll (in terms of final result) is all ones except for a single two, yielding 2 as the final result. Apr 14, 2021 at 9:09
• BTW, this is very similar to the dice rolling mechanic in the "Simple D6" system, which I really like, except that in that system you only add the number of duplicates if the highest roll is a natural 6. This avoids some of the quirks of your system, like [5, 5, 5] being a better roll than [5, 5, 6] and 1 being impossible to roll on more than one die. Apr 14, 2021 at 9:23
• I'm not sure I agree with your statement of "the odds of rolling enough lower duplicates to outperform a roll of higher value is low enough to ignore", especially for larger numbers of dice. Apr 14, 2021 at 14:41

There's a couple little tricks we can use here. Firstly, we want to look at the individual values of a roll, so we need to cast the roll to a sequence. We do this by appending :s to the parameter name in the function declaration.

We then make use that such a sequence will (by default) be sorted descending, so we can get the highest value as 1@DICE. The third trick to recognize that the number of duplicates is one less than the number of that value, for which there is an inbuilt function [count value in sequence]. Assembling this all together we get:

function: roll DICE:s add dupecount to highest {
result: 1@DICE + ([count 1@DICE in DICE] - 1)
}


You can use the function like this:

output [roll 3d6 add dupecount to highest]


Which produces: We can then sanity check our result. Our highest value is 8 from getting [6, 6, 6] and we'll see that this has the probability of 1 over 216 (from 6$$\^3\$$). The the minimum of 2 is from [2, 1, 1] and the two other permutations of that result for a probability of 3/216. (I'm mostly sharing that to show a simple way to check the result, manually calculating the for the extreme outcomes is usually much easier.)

Beside Someone_Evil's excellent answer, I would like to add a mathematical contribution, in order to generalize the above rule for large number of $$\X\$$ and for different dice.

On a first hand, observe that the score $$\v\$$ under the above rules is given by $$\begin{equation} v = h+s-1 \end{equation}$$ where $$\h\$$ is the highest roll among the dice and $$\s\$$ is the number of realizations of such roll. Consider for example $$\v=3\$$ within rolling 3d6: the outcomes that provide with such result are

• (1,1,1)
• (2,2,1), (1,2,2), (2,1,2)
• (3,1,1), (1,3,1), (1,1,3), (3,2,1), (3,1,2), (2,3,1), (1,3,2), (2,1,3), (1,2,3), (3,2,2), (2,3,2), (2,2,3)

Observe that the last one can be summarized in just 3 cases (3,$$\x\$$,$$\y\$$), ($$\x\$$,3, $$\y\$$), ($$\x\$$,$$\y\$$,3), where $$\x<3\$$ and $$\y<3\$$.

Hence:

• $$\h=1\$$, $$\s=3\$$ $$\\to\$$ $$\v=1+3-1=3\$$
• $$\h=2\$$, $$\s=2\$$ $$\\to\$$ $$\v=2+2-1=3\$$
• $$\h=3\$$, $$\s=1\$$ $$\\to\$$ $$\v=3+1-1=3\$$

Obviously, when the highest roll is 4 or more, one can not get a score of 3. Compute now the probabilities of the above realizations, where $$\P(v|h=k)\$$ means the probability to have a score of $$\v\$$ when the highest result from the rolls is $$\k\$$:

• $$\h=1\$$: $$\P(3|h=1)=P(\mbox{get all 1s})=(1/6)^3\$$

• $$\h=2\$$: $$\begin{eqnarray} P(3|h=2)&=&P(\mbox{get exactly 2 2s})\cdot P(\mbox{get 1 roll less then 2})\cdot(\mbox{num of equivalent realizations})\\ &=& \left(\frac16\right)^2\cdot F(1)^1\cdot 3 = \left(\frac16\right)^33 \end{eqnarray}$$ where $$\F(x)\$$ is the cumulative distribution function: $$\F(x)\$$ gives the probability to get a result less or equal than $$\x\$$, hence if we want to compute the probability to get a number strictly less than 2 we have to compute $$\F(1)\$$.

• $$\h=3\$$: $$\begin{eqnarray} P(3|h=3)&=&P(\mbox{get exactly one 3})\cdot P(\mbox{get 2 rolls less then 3})\cdot(\mbox{num of equivalent realizations})\\ &=& \left(\frac16\right)^1\cdot F(2)^2\cdot 3 = \left(\frac16\right)\left(\frac13\right)^23 \end{eqnarray}$$

• $$\h\geq 4\$$: $$\P(3|h\geq4)=0\$$

The probability of getting a score of 3 is then $$P(3) = \sum_{i=1}^6P(3|h=i)$$

We are now able to generalize the above pattern: let $$\f(x)\$$ be the probability of getting $$\x\$$ with a roll, $$\F(x)\$$ the cumulative distribution function and $$\\begin{pmatrix}n\\k\end{pmatrix}\$$ the binomial coefficient, which gives all the ways of picking up $$\k\$$ objects among $$\n\$$ without taking into account the order. Let's consider a die with $$\D\$$ faces: the probability of scoring $$\v\$$ with a XdD roll is given by $$P(v) = \sum_{i=1}^D P(v|h=i) = \sum_{i=1}^D \begin{pmatrix}X\\s_i\end{pmatrix}f(i)^{s_i} F(i-1)^{(X-s_i)}$$ where $$\s_i\$$ is the number of successes needed when the highest roll is $$\i\$$ and $$\F(0)=0\$$. The term $$\f(i)^{s_i}\$$ is the probability of getting exactly $$\s_i\$$ successes, $$\F(i)^{X-s_i}\$$ is the probability that the remaining rolls are lower than $$\i\$$. Finally, the binomial coefficient takes into account the equivalent sequences.

As an example, consider the case of rolling 5d4:

• P(2) = 0.49%
• P(3) = 8.79%
• P(4) = 48.34%
• P(5) = 30.86%
• P(6) = 9.86%
• P(7) = 1.56%
• P(8) = 0.10%

The figure down below, instead, depicts the distribution when the number of rolled d6 ranges from 3 to 60: the yellower, the higher the probability. The computation has been done in Python. As a curiosity, as the number of rolled dice increases, the distribution resembles a Gaussian one (with $$\r^2\$$ close to 0.99), as it is clear from the figure below (credits to Kieran Mullen for pointing out that a 3D plot could be more clear). Indeed, as Someone_Evil pointed out in the comments, for large $$\N\$$ (about 30-35) the probability to get at least one 6 is close to 1: hence, the above distribution is simply a Binomial one. Moreover, as $$\N\$$ approaches infinity (in real life, it means that it is large, in this case about 30) the binomial distribution is approximated by a Gaussian one. • golf clap and in particular, the illustration (the time and effort you put into this answer is very much appreciated) Apr 15, 2021 at 14:18
• Thanks @KorvinStarmast! It took me a while, but I had fun doing it! I believe that sometimes one needs some mathematical insights on this dice/probability topics, because Anydice is very powerful but it may limit the comprehension of rolling systems such as the one depicted by the OP. Apr 15, 2021 at 14:57