For some reason you are not able to describe your game clearly.
Now it sounds like you want to keep rolling until you DON'T get a six. So if you roll a non-6 right away, then that's the value of your roll. If, for example, you roll 6, 6, 6, 3, then the value of the roll would be 21.
In that case, the program is the following:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int roll() {
int result = 0, z;
do {
z = rand() % 6 + 1;
result += z;
} while (z == 6);
return result;
}
int main() {
srand(time(NULL));
const int reps = 1000000;
int sum = 0;
for (int i = 0; i < reps; ++i)
sum += roll();
printf("%f\n", (double)sum / reps);
return 0;
}
I'm not sure how to do the calculation, but here's an approximation, recalling that 3 is the average of the numbers 1 to 5. Also 6's occur 1/6 of the time, 2 6's occur 1/(6*6) times, 3 6's occur 1/(6*6*6) times, etc:
Code:
(1 - 1/ 6) * 3 +
(1/ 6 - 1/ 36) * (6 + 3) +
(1/ 36 - 1/ 216) * (6 + 6 + 3) +
(1/ 216 - 1/1296) * (6 + 6 + 6 + 3) +
(1/1296 ) * (6 + 6 + 6 + 6 + 3)
= 4.199074...
Or, using ^ as the exponentiation symbol:
Code:
(1 / 6^0 - 1 / 6^1) * (6*0 + 3) +
(1 / 6^1 - 1 / 6^2) * (6*1 + 3) +
(1 / 6^2 - 1 / 6^3) * (6*2 + 3) +
(1 / 6^3 - 1 / 6^4) * (6*3 + 3) +
(1 / 6^4 ) * (6*4 + 3)
I.e., the sum for i from 0 to infinity of
(1 / 6^i - 1 / 6^(i+1)) * (6 * i + 3)
As a program, adding the first 10 terms:
Code:
#include <stdio.h>
int main() {
double sum = 0, sixes = 1;
for (int i = 0; i < 10; ++i, sixes *= 6)
sum += (1.0 / sixes - 1 / (sixes * 6)) * (6 * i + 3);
printf("%f\n", sum); // prints 4.199999
}
So the mathematical answer seems to be converging to 4.2.