Thread: Dice

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    24

    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?
    Last edited by jasmin89; 11-08-2020 at 01:35 AM.

  2. #2
    Registered User
    Join Date
    Oct 2020
    Posts
    18
    else

    int i = roll ();

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

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    76
    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. #4
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    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?
    Last edited by jasmin89; 11-08-2020 at 03:49 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    You return result within the do while loop. That's certainly wrong because it means your loop only ever runs once.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    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. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    Quote Originally Posted by jasmin89
    Do you mean that i must the write the return result outside of the loop.
    Yes.

    Quote 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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    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. #9
    Registered User
    Join Date
    May 2012
    Posts
    502
    Quote Originally Posted by jasmin89 View Post
    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.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    1,126
    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;
    }
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  11. #11
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    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;
    }
    Last edited by jasmin89; 11-09-2020 at 12:32 AM.

  12. #12
    Registered User
    Join Date
    Dec 2017
    Posts
    1,126
    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.
    Last edited by john.c; 11-09-2020 at 01:57 AM.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  13. #13
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    Quote Originally Posted by john.c View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dice Program
    By M_A_T_T in forum C Programming
    Replies: 4
    Last Post: 01-31-2013, 03:52 PM
  2. Dice Roller
    By Bobnine in forum C Programming
    Replies: 3
    Last Post: 05-19-2008, 09:45 PM
  3. Dice Simualtion
    By sillyman in forum C Programming
    Replies: 2
    Last Post: 04-22-2008, 01:43 PM
  4. dice problem
    By goran00 in forum C Programming
    Replies: 16
    Last Post: 02-10-2008, 06:59 PM
  5. C++ dice roll need help
    By catdieselpow in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2007, 01:32 PM

Tags for this Thread