1. Dice

Hi,

i am writing a programm which what should have the following functionality:

The computer is dicing, if the computer has diced no 6 we return the number of points, if he rolled a 6 he roll again, the computer continues to roll the dice until it has no more 6.

I have tried to programming this:

Code:
```    #include <stdio.h>
#include <stdlib.h>

int roll(void)
{
return (rand() % 6) + 1;
}

int main()
{ srand(time(NULL));

int i = roll();

if (i < 6) {
printf("Diced number = %d\n", i);

else                                    // if there is a 6  you can dice again

int i = roll();

while (i < 6) {                         // The computer continues to roll the dice until it has no more 6
printf("Count = %d\n", i);

}

return 0;
}```
It would be very nice if you could help me a little. Because i get an error at line 18 with the statement. Did I put the brackets wrong?

2. else

int i = roll ();

your pgoram only rolls the dice ONE MORE TIME if the first roll is not 6.

3. Maybe like this:
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int roll()
{
int dice;

do
{
dice = rand() % 6 + 1;

}while(dice == 6);

return dice;
}

int main()
{
srand(time(NULL));

int dice = roll();

printf("Dice = %d", dice);

return 0;
}```

4. Thanks for the help. But i have a modified my program a little bit.

In the main() i am calculating the dicing for 1000 times and I am calculating the average. (this average is my result)

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int roll(void)
{
return (rand() % 6) + 1;
}

int croll(void)
{

int result=0;
int z = 0;
do {
int z = roll();
result += z;

return result;
}  while(z != 6);
return result;

}

int main() {
srand(time(NULL));
int sum = 0;
int steps = 1000;
for (int i = 0; i < steps; i++)
sum += croll();
printf("%f\n", (double)sum / steps );
}```
But it seems like that my result is wrong. Because the average roll is 3.5 points with one roll. But i can roll it again when i have a 6. So in my case the average result must be higher than 3.5 Does anybody know what is wrong on my code?

5. You return result within the do while loop. That's certainly wrong because it means your loop only ever runs once.

6. Ok, for me it is not really clear. Do you mean that i must the write the return result outside of the loop. Like this:

Code:
```int croll(void)
{

int result=0;
int z = 0;
do {
int z = roll();
result += z;

}  while(z < 6);
return result;

}```
When i do this i get no result. Thank you for your help

7. Originally Posted by jasmin89
Do you mean that i must the write the return result outside of the loop.
Yes.

Originally Posted by jasmin89
When i do this i get no result.
That's probably because you're declaring z twice, hence the inner z shadows the outer z, but you're using the outer z to control the loop. Consequently, the outer z never changes its value from 0, so the loop condition is always true. You should do something like this:
Code:
```int croll(void)
{
int result = 0;
int z;
do {
z = roll();
result += z;
}  while (z < 6);
return result;
}```

8. Ok but when i change this in the code i get numbers higher than 6... But i want to have only numbers from 1-6.

9. Originally Posted by jasmin89
Ok but when i change this in the code i get numbers higher than 6... But i want to have only numbers from 1-6.
If 6 means "roll again and add six" then this is impossible.

The object of the exercise seems to be to get the mean. This isn't entirely simple, and it's one of those things that is maybe easier to calculate by computer simulation than mathematically.

10. If you want to keep rolling until you get a 6, summing all the rolls up to but not including the 6, then the average would be 15. That's because the average of the possible values is (1+2+3+4+5) / 5 = 3, and you expect on average 5 rolls before you get a 6: 3 * 5 = 15.

If you also want to include the final 6 than you just add that in: 15 + 6 = 21.
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int roll() {
int result = 0;
while (1) {
int z = rand() % 6 + 1;
if (z == 6) break;      // Swap these two lines ...
result += z;            // ... to include the 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;
}```

11. Yes, sure i want to add the 6. Because i want to simulate the average also when i diced a 6. (The rule of the game is if i diced a 6 i can dice it again -Until there is no more 6)

But i think i had to calculate the average already for the loop.

Then later on in the main() i can simulate the dice for many times.

I have add this in the code but I am not really sure if i have made this right.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int roll() {
int num = 0;
double result = 0;
while (1) {
int z = rand() % 6 + 1;
if (z == 6) break;      // Swap these two lines ...
num++;                  // count the number of times a while loop is executed
result += z/num;            // ... to include the 6.

}
return result;        // Calculation of the average depending on the number of while loop passes
}

int main() {
srand(time(NULL));
const int reps = 10;
int sum = 0;
for (int i = 0; i < reps; ++i)
sum += roll();
printf("%f\n", (double)sum / reps);
return 0;
}```

12. 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.

13. Originally Posted by john.c
For some reason you are not able to describe your game clearly.

If, for example, you roll 6, 6, 6, 3, then the value of the roll would be 21.
Yes i want to have something like that. Thank you for your help, very friendly. Now it is clear for me.