# Dice rolling problem

A game is played with $$n$$ dice, and an additional parameter $$r<=6$$. The game has one player who must throw all $$n$$ dice on each round and add the score to the total. The game works as follows:

1. The player starts with a score of zero.
2. On each round, the $$n$$ dice are thrown and the sum of the dice is added to the player's total score.
3. If the number on every dice is no larger than $$r$$, the game ends at the current total score.
4. Otherwise the game continues to the next round. The game continues through multiple rounds until the value of every dice is no larger than $$r$$. For example, if $$n=1$$ and $$r=2$$, then the sequence of throws: $$4,6,3,2$$ will score $$4+6+3+2=15$$. The game ends because the value of the die is no larger than 2 . If $$n=2$$ and $$r=3$$, then the sequence of throws: $$(4,1),(3,2)$$ will score $$4+1+3+2=10$$. The game ends because the values of the dice 3,2 are both no larger than 3 . Your task is to write a function to calculate the expected total score of the game. The function should take two arguments, $$n$$ and $$r$$, and sutput the expected score as a floating point number.

What I have done is assume E[total score of n dice] as f(n), now by total expectation formula:

$$f(n) = (\frac{6-r}{6})^{n} (\frac{n(r+7)(6-r)}{2} + f(n)) + (1- (\frac{6-r}{6})^{n})\frac{n(1 +r)r}{2}$$. Does that right?

Your $$\left(\frac{6-r}{n}\right)^n$$ looks like the probability all the rolls are greater than $$r$$, while your rule 3 suggests you should look at $$\frac{6^n-r^n}{6^n}$$ as the probability they are not less than or equal to $$r$$. Your $$\frac{n(r+7)(6-r)}{2}$$ looks strange too.

How I would do it:

• The expected sum of the rolls is $$\frac72n$$
• The probability of them all being less than or equal to $$r$$ is $$\frac{r^n}{6^n}$$
• and the probability of them not all being less than or equal to $$r$$ is is $$\frac{6^n-r^n}{6^n}$$
• Conditioned on all being less than or equal to $$r$$, the expected sum is $$\frac{r+1}{2}n$$
• so conditioned on not all being less than or equal to $$r$$, the expected sum is $$\frac{6^n\frac72n -r^n \frac{r+1}{2}n }{6^n-r^n}$$
• giving you an expression for the overall expectation $$f(n) = \frac{6^n-r^n}{6^n}\left(\frac{6^n\frac72n -r^n \frac{r+1}{2}n }{6^n-r^n} +f(n)\right)+\frac{r^n}{6^n}\frac{r+1}{2}n$$
• which then simplifies to $$f(n) = \frac{6^n}{r^n}\frac72n$$

and I would want to check this by simulation. It such a simple expression that there may be a quicker route.

So here is an example of a simulation using R:

game <- function(n, r){
total <- 0
rolls <- rep(7, n)
while(any(rolls > r)) {
rolls <- sample(1:6, n, replace=TRUE)
total <- total + sum(rolls)
}
return(total)
}

n <- 5
r <- 3
cases <- 10^5
set.seed(2023)
sims <- replicate(cases, game(n, r))
mean(sims)
# 561.412
(6^n/r^n)*(7/2)*n
# 560


which looks good, allowing for simulation noise.

The shorter argument is to use the optional stopping theorem. The expected sum from rolling $$n$$ dice is $$\frac72 n$$. So if we subtract this each time we roll, the expected total would be $$0$$, whether we stop or not. Since the probability of stopping at any particular round is $$\frac{r^n}{6^n}$$, we will stop in finite time with probability $$1$$ and the expected number of rounds is $$E[N]= \frac{6^n}{r^n}$$, so the optional stopping theorem tells us $$E[X-N\frac72n]=0$$ and so $$E[X]-E[N]\frac72n=0$$ i.e. $$E[X]=\frac{6^n}{r^n}\frac72n$$.

• Why conditioned on not all being less than or equal to $r$, the expected sum is $\frac{6^n\frac72n -r^n \frac{r+1}{2}n }{6^n-r^n}$? Oct 1 at 1:28
• Maybe I have figure that that since E[X] = $\frac{7}{2}n$ = E[X|all being less than or equal to r]p(all being less than or equal to r) + E[X| not all being less than or equal to r]p( not all being less than or equal to r)Thus, we have $$\frac{7}{2}n = \frac{r + 1}{n}\frac{r^n}{6^n} + \frac{6^n - r^n}{6^n} E[X| \text{not all being less than or equal to r}]$$ Does that right? Oct 1 at 1:36
• @ZhihaoXu I used $E[Z]=E[Z \mid A]P(A)+E[Z \mid A^c]P(A^c)$ so $E[Z \mid A^c] = \frac{E[Z]-E[Z \mid A]P(A)}{P(A^c)}$ to get $\frac{6^n\frac72n -r^n \frac{r+1}{2}n }{6^n-r^n}$ Oct 1 at 10:50
• Yes I use the same one too. Thanks for your kind help! Oct 1 at 14:09

I have seen another explanation from my friend. Assume X is the sum. Denote Y as the dice number for one time, $$N$$ as the number of rolls. \begin{aligned} E[X] = E[nYN] = E[nY]E[N] =\frac{7}{2}n\frac{6^n}{r^n} \end{aligned} where we have used rolling n dices is independent of number of retry and We can view N as a Geometric distribution(p) with expectation $$\frac{1}{p}$$.

Updates: I find a general and easier solution using wald's identity: If $$X_{1}, ..., X_{N}$$ is iid and has finite mean and N is a stopping time with $$E[N] <\infty$$, then

$$E[X_{1} + ... + X_{N}] = E[N]E[X_{1}]$$

Proof:

\begin{aligned} E[X_{1} + ... + X_{N}] &= E\left[\sum^{\infty}_{n = 1}X_{n}\mathbb{1}_{N\ge n}\right] \\ &= \sum^{\infty}_{n = 1}E[E[X_{n}\mathbb{1}_{N\ge n}|\mathbb{F}_{n-1}]] \\ &= \sum^{\infty}_{n = 1}E[\mathbb{1}_{N\ge n}E[X_{n}|\mathbb{F}_{n-1}]] \\ &= \sum^{\infty}_{n = 1}E[\mathbb{1}_{N\ge n}E[X_{n}]] \\ &=E[X_{1}] \sum^{\infty}_{n = 1}E[\mathbb{1}_{N\ge n}] \\ &= E[X_{1}] E[N] \end{aligned}

• This gives the same answer as mine. A possible difficulty with this is that the number of rolls is not independent of the values rolled, so you need a different argument for $E[X]=E[nY]E[N]$: for example if you roll $n$ $1$s with sum $n$ then you stop while if you roll $n$ $6$s with sum $6n$ (and $r<6$) then you go on. Oct 1 at 10:43
• OK - your argument needs a suitable martingale (subtract $\frac72 n$ each round) so you can apply the optional stopping theorem. Oct 1 at 12:24