Write a C++ program to roll 3 dice, show the values of these dice, and show the total

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alexben
    New Member
    • Feb 2010
    • 3

    Write a C++ program to roll 3 dice, show the values of these dice, and show the total

    Please I need help with this.
    Write a C++ program to roll 3 dice, show the values of these dice, and show the total value of these dice.?

    I actually did this but I am very sure it is wrong.

    Code:
     #include <iostream>
       #include <cstdlib>
       #include <time.h>
        
      using namespace std;
        
     int rollDice();
       
     int main(int argc, char* argv)
      {
          int numRolls;
           int arrRolls[6];
           int dieRoll;
      
           srand((unsigned)time(0));
       
          cout << "How many times would you like to roll the dice?" << endl;
           cin >> numRolls;
        
         for (int k = 0; k < 6; k++)
          {
               arrRolls[k] = 0;
           }
       
           for (int i = 1; i <= numRolls; i++)
          {
              dieRoll = rollDice();
             cout << "Die Roll " << i << ": " << dieRoll << endl;
              arrRolls[dieRoll - 1]++;
          }
        
         for (int j = 0; j < 6; j++)
         {
               cout << "The number " << j + 1 << " was rolled " << arrRolls[j] << " times." << endl;
           }
     }
        
       int rollDice()
       {
           int result;
        
           result = (rand() % 6) + 1;
        
           return result;
       }
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    hmmm...think that the three dice should be rolled simultaneously.
    1. So you want to generate three random integers with values between 1 and 6 inclusive.
    2. Output required is the value of each die and the sum of the three dice.
    Can you do 1.? (not sure I can) but 2. should then be pretty simple.
    BTW what happened when you compiled and ran your program ... any warnings or errors?

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Line 42

      result = (rand() % 6) + 1;

      Is a very poor way to obtain a random result between 1 - 6 because the low bits of the standard library random number generator are often not very random but this relies solely on the low bits.

      Read this

      Comment

      • alexben
        New Member
        • Feb 2010
        • 3

        #4
        Originally posted by whodgson
        hmmm...think that the three dice should be rolled simultaneously.
        1. So you want to generate three random integers with values between 1 and 6 inclusive.
        2. Output required is the value of each die and the sum of the three dice.
        Can you do 1.? (not sure I can) but 2. should then be pretty simple.
        BTW what happened when you compiled and ran your program ... any warnings or errors?
        This was the ouput when I compiled it. That doesn't look like what we were told to do. Help me with the second part, I would try to figure out the first part.

        How many times would you like to roll the dice?
        3
        Die Roll 1: 1
        Die Roll 2: 2
        Die Roll 3: 1
        The number 1 was rolled 2 times.
        The number 2 was rolled 1 times.
        The number 3 was rolled 0 times.
        The number 4 was rolled 0 times.
        The number 5 was rolled 0 times.
        The number 6 was rolled 0 times.
        Press any key to continue . . .

        Comment

        • alexben
          New Member
          • Feb 2010
          • 3

          #5
          Originally posted by Banfa
          Line 42

          result = (rand() % 6) + 1;

          Is a very poor way to obtain a random result between 1 - 6 because the low bits of the standard library random number generator are often not very random but this relies solely on the low bits.

          Read this
          Okay!Thank you. I have read it. I will try to do it over again and see how it comes out.

          Comment

          Working...