Count the highest number of matching results

anydice

For a Xd6 dice pool I want to know probabilities of getting a specific number of same outcomes, for all numbers from 2 to X.

For example, I roll 5d6 and get 2, 3, 2, 2, 4 – the result is "3" because there are three 2s. But what was the chance of getting "3" for the 5d6 pool?

For 1, 6, 1, 6, 1 – two 6s, three 1s – the result should be "3", because 3 is the highest.

For 2, 2, 3, 3, 6 the result should be "2", et cetera.

The matching outcome itself is irrelevant, all I want to know is the highest duplicates number. I’ve found this question – How do I count the number of duplicates in anydice? – but it counts all duplicates, not the highest number of them.

Best Answer

Here's a function to count duplicates in a roll:

function: count highest dupes in DICE:s {
  MAX: 0
  loop X over {1..1@DICE} {
    COUNT: X = DICE
    if COUNT > 1 & COUNT > MAX { MAX: COUNT }
  }
  result: MAX
}

Let's step through the function.

function: count highest dupes in DICE:s {

The function declaration takes one parameter, the dice pool rolled, and casts it to a fixed sequence for inspection.

  MAX: 0

Initialises a counter variable, MAX, to remember what the maximum number of duplicates is.

  loop X over {1..1@DICE} {

Loops a variable X over the sequence of values from 1 to the highest individual die result in the pool. Thanks to the automatic sorting of dice cast to a sequence, we know that the first number in the sequence (1@DICE) will be the highest die, and we don't need to check any numbers higher than that.

    COUNT: X = DICE
    if COUNT > 1 & COUNT > MAX { MAX: COUNT }

For each value of X, count the number of matches in the pool. If there is more than one match and the number of matches is higher than the currently known maximum, set that to be the new maximum.

  }
  result: MAX
}

When the loop ends, the result is whatever the maximum number of duplicates was.

Your specific case of rolling 5d6 would be invoked by:

output [count highest dupes in 5d6]
Related Topic