I'm playtesting a game in which, on a turn, you take two statistics, n and s, and then roll nds (that is, n dice of s sides each), summing the result. If all the dice have the same result, then you roll again, adding the new result to the previous sum, and you keep going until not all dice are the same. For example, suppose n = 3 and s = 6. Then, you would start by rolling 3d6:
Example 1: Suppose you get the result {1, 3, 4}. Then you would stop immediately, and your sum for the turn would be 1 + 3 + 4 = 8.
Example 2: Suppose you had instead gotten the result {3, 3, 3}. Since all the dice are the same, you would roll again, and suppose this time you get {2, 2, 2}. Again, all the dice are the same, so you roll again, and this time you get {1, 1, 4}. Since not all dice are the same, you would now stop, and your sum for the turn would be 3 + 3 + 3 + 2 + 2 + 2 + 1 + 1 + 4 = 21.
What would be a formula in terms of n and s for calculating the expected value for a turn?
And what would be the expected value for a turn if the rules were modified so that, rather than requiring all the dice to match, you roll again and sum to the previous roll(s) whenever any dice among those rolled match? So in the second example above, you would continue rolling on the third roll since two dice came up with the value 1. (Obviously, with the modified rule, if n is greater than s, then the expected value would be infinite since you are guaranteed to always have a match on each roll in that case, but I would still like to know about the expected value for the cases where n is less than or equal to s.)
Note that in both cases, the condition for getting another roll is symmetric with respect to the values on the dice. You’re not treating any of the $s$ results of a die any different than any other, so the expected value of each die conditional on getting another roll (or not) is the same as the unconditional expected value of each die, $\frac{s+1}2$. That makes it much more straightforward to calculate the expected value of the sum, since we just have to multiply the expected number of dice rolled by the expected value of each die, without worrying about a dependence of the latter on the former.
The expected number of times you try something that occurs with probability $p$ until it occurs is $\frac1p$.
In the first case, the probability to get all numbers the same is $s^{-(n-1)}$, so the expected value of the sum is
$$ n\cdot\frac{s+1}2\cdot\frac1{1-s^{-(n-1)}}=n\cdot\frac{s+1}2\cdot\frac{s^{n-1}}{s^{n-1}-1}\;.$$
In your example with $n=3,s=6$ this would be
$$ 3\cdot\frac{6+1}2\cdot\frac{6^{3-1}}{6^{3-1}-1}=\frac{54}5=10.8\;, $$
compared to $10.5$ without rerolls.
In the second case, the probability to get all numbers different is $\frac{s!}{(s-n)!}s^{-n}$, so the expected value of the sum is
$$ n\cdot\frac{s+1}2\cdot\frac1{\frac{s!}{(s-n)!}s^{-n}}=n\cdot\frac{s+1}2\cdot\frac{(s-n)!s^n}{s!}\;.$$
In your example with $n=3,s=6$ this would be
$$ 3\cdot\frac{6+1}2\cdot\frac{(6-3)!\cdot6^3}{6!}=\frac{189}{10}=18.9\;, $$
again compared to $10.5$ without rerolls.