Exact

Many dice

Seven dice have 279,936 ordered rolls and nine have over ten million. A product of independent parts is kept as its parts, and each question is answered in the cheapest exact way: one roll by multiplying, a total by convolution, any other event by streaming the rolls one at a time and counting.

A product is its parts

import { die, dice, coin, finite, rng } from "@ratmath/stats";

const nine = dice(9);          // immediate: nothing is listed
nine.count;                    // 10077696n
nine.factors.length;           // 9, each one die()
nine.p([1, 2, 3, 4, 5, 6, 1, 2, 3]);   // 1/10077696
nine.marginal(4);              // die() itself
nine.marginal(0, 8);           // two dice, still implicit

product, power and dice keep the independent parts. The probability of one outcome is the product of the parts' probabilities,

P(a1,…,ak)=∏i=1kPi(ai), P(a_1, \dots, a_k) = \prod_{i=1}^{k} P_i(a_i),

and a marginal of an independent product is just the part, so neither needs the other outcomes. count is the exact number of outcomes as a bigint; size is the same as a number.

Totals by convolution

dice(7).sum().p(24);            // 24017/279936
dice(20).sum().p(70);           // exact, from 20 convolutions of six faces
die().sumOf(20).p(70);          // the same, by repeated doubling

The sum of independent parts has the convolution of their distributions,

P(X+Y=s)=∑xP(X=x) P(Y=s−x), P(X + Y = s) = \sum_{x} P(X = x)\,P(Y = s - x),

so sum() works part by part and the cost grows with the number of totals, not the number of rolls. Twenty dice have 620≈3.7×10156^{20} \approx 3.7 \times 10^{15} rolls but only 101 totals.

Any event, by streaming

// the first die equals the last, and some die shows a six
nine.prob((r) => r[0] === r[8] && r.includes(6));       // 1288991/10077696
nine.prob((r) => r.includes(6), (r) => r[0] === 1);     // 1288991/1679616: conditional, one pass
nine.map((r) => new Set(r).size);                          // how many different faces show
for (const [roll, p] of dice(2).each()) { /* one at a time */ }

An event that is not built from the parts has to look at every roll, but it only needs one at a time. The rolls are visited in order like an odometer, each with an integer weight over a common denominator (for fair dice every weight is 1), the weights of the rolls in the event are added as integers, and the total is divided once at the end:

P(A)=#{ω∈A}69for fair dice, andP(A)=∑ω∈Aw(ω)Din general. P(A) = \frac{\#\{\omega \in A\}}{6^9} \quad\text{for fair dice, and}\quad P(A) = \frac{\sum_{\omega \in A} w(\omega)}{D} \quad\text{in general.}

Memory stays flat whatever the number of dice; time is about a tenth to a fifth of a second per million rolls on a laptop-class machine. map streams the same way and keeps only the distinct values of the function, prob(event, given) counts both events in one pass, and mean, variance, covariance and correlation of functions go through map.

Sampling and listing

nine.sample(rng(1));            // nine faces, drawn part by part
dice(3).entries.length;         // 216: listed on first use
nine.entries;                   // refused: more than two million outcomes

A sample draws one integer below the common denominator and reads each part off it in turn, so a seeded sample is the same roll the listed table would give. Listing (entries, outcomes, table, and given, which lists the outcomes it keeps) is still limited to two million outcomes; everything above works beyond that.

Try itEdit the code and run it with @ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs