9
\$\begingroup\$

I feel like I'm tweaking, cause everything I'm reading in the documentation makes me think I'm doing it right.

I'm trying to characterize a roll of 2d6 as a sequence that returns {R, C}, where R is the sum of numbers rolled and C is the number of 6s rolled. I can't, because for some reason every time I try and access this sequence, it just sums all elements of the sequence and gives it to me as a number...

I'm not sure if I'm just missing something, or it's because it's already a die that I'm accessing, but then, if it's a roll, it should be returning a die with sequences as results. Maybe it's because calling a function like this on dice doesn't allow a sequence as the return type. I can't figure it out. Here's the link.

\$\endgroup\$
1
  • \$\begingroup\$ Everyone seems to have a dice library these days but because I find anydice hard, here's your question answered using mine: colab.research.google.com/drive/… \$\endgroup\$ Commented Nov 18, 2025 at 21:08

1 Answer 1

9
\$\begingroup\$

If a function call results in any arguments being expanded, i.e. a die or pool was sent to a parameter of type sequence or number, the result is always a die. However, AnyDice does not have dice whose outcomes are sequences, only numbers. Thus, in this case AnyDice performs its standard reduction of a sequence to a number, which is to sum the values.

Using digits

If we want to return multiple values from a function in AnyDice, the standard method is to use digits. So instead of

 S: {A+B, C}
 result: S

we can do something like

 result: 1000 + (A + B) * 10 + C

If we want to extract A + B or C, we can send the resulting die to another function that uses the @ operator on a number:

function: second and third digit N:n {
 result: 2@N * 10 + 3@N
}

function: fourth digit N:n {
 result: 4@N
}

output [second and third digit [roll 1d6 1d6]]
output [fourth digit [roll 1d6 1d6]]

You can find the example program here.

Pitfalls

Position order

Why did we add 1000 at the beginning? If we omitted the 1000 and instead tried to extract A + B from the first and second digits, we would get the wrong answer when A + B is less than 10: in this case the tens digit would become the first digit. We could index from the lowest first by using

set "position order" to "lowest first"

but note that this this also affects the order of sorting.

The @ operator

Why did we use a separate fourth digit function rather than applying the @ operator directly? We need to do this because the @ operator works differently on dice (really dice pools) than on numbers. If we had done

output 4@[roll 1d6 1d6]

the result is 0. This is because [roll 1d6 1d6] is a "pool" of a single die, and we are trying to get the fourth-highest result in the "pool", which doesn't exist, so AnyDice gives us 0.

output 2@[roll 1d6 1d6] + 3@[roll 1d6 1d6]

would have been doubly wrong; even if each @ had performed the desired digit extraction, it would have taken the tens place of the sum from one roll of the pool and the ones place from a second, independent roll of the pool, thus potentially resulting in a "sum" of 19 from 2d6! Sending a single pool to a function also solves this problem.

Sequence expansion

An alternative way to implement roll is to use AnyDice's expansion of sequence-type parameters, which expands a dice pool into every possible sorted sequence of numbers it could roll.

function: roll ROLLS:s {
 result: 1000 + ROLLS * 10 + [count 6 in ROLLS]
}
output [roll 2d6]

This has the advantage of working for up to 9 dice; past that we would need to allocate another digit for the number of 6s and possibly the sum of the dice. Note that here + ROLLS * 10 implicitly sums the sequence.

Icepool

My own Icepool Python probability package does allow tuple-valued dice:

from icepool import d, map

def sum_and_sixes(rolls):
    return sum(rolls), rolls.count(6)

result = map(sum_and_sixes, d(6).pool(2))
output(result)
output(result.marginals[0])
output(result.marginals[1])

You can try this in your browser here.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.