Forum Discussion
Count how many pairs of dice sum >= 7?
- Jan 17, 2025
Absolutely. REDUCE can be used to iterate through the number of dice, adding the SEQUENCE of sides (vertical vector) to the previous transposed results using TOROW (horizontal vector). The lambda definition could be something like this:
=LAMBDA(num_of_sides,num_of_dice,operator,value, LET( n, SEQUENCE(num_of_sides), m, IF(num_of_dice = 1, n, REDUCE(n, SEQUENCE(num_of_dice - 1), LAMBDA(a,i, TOROW(a) + n))), t, CHOOSE( XMATCH(operator, {"<";"<=";"<>";"=";">";">="}), m < value, m <= value, m <> value, m = value, m > value, m >= value ), SUM(--t)/COUNTA(t) ) )
You could name it DICEPROBλ, for example, then use it as follows:
=DICEPROBλ(6,2,">=",7)
Or:
=DICEPROBλ(6,3,">",9)
Or:
=DICEPROBλ(6,7,"<",25)
And so on and so forth.
This function returns the probability. If you want it to return the count instead, simply remove /COUNTA(t) from the last line of the function definition.
Note: the PERMλ function was a generic permutation generator I had saved from a previous project. It works but is not the most efficient method for this scenario. Thanks for prompting me to rethink it! 😉
Have just tried this after reading yours and it makes sense now - apologies for my oversight. Is there still not a way to do it using lambda with a sequence and a transposed sequence?
Absolutely. REDUCE can be used to iterate through the number of dice, adding the SEQUENCE of sides (vertical vector) to the previous transposed results using TOROW (horizontal vector). The lambda definition could be something like this:
=LAMBDA(num_of_sides,num_of_dice,operator,value,
LET(
n, SEQUENCE(num_of_sides),
m, IF(num_of_dice = 1, n, REDUCE(n, SEQUENCE(num_of_dice - 1), LAMBDA(a,i, TOROW(a) + n))),
t, CHOOSE(
XMATCH(operator, {"<";"<=";"<>";"=";">";">="}),
m < value, m <= value, m <> value, m = value, m > value, m >= value
),
SUM(--t)/COUNTA(t)
)
)
You could name it DICEPROBλ, for example, then use it as follows:
=DICEPROBλ(6,2,">=",7)
Or:
=DICEPROBλ(6,3,">",9)
Or:
=DICEPROBλ(6,7,"<",25)
And so on and so forth.
This function returns the probability. If you want it to return the count instead, simply remove /COUNTA(t) from the last line of the function definition.
Note: the PERMλ function was a generic permutation generator I had saved from a previous project. It works but is not the most efficient method for this scenario. Thanks for prompting me to rethink it! 😉
- Etizaz24Jan 17, 2025Brass Contributor
Thank you for this! Very interesting to see.