Dice random generator problem

This is a discussion on Dice random generator problem within the C++ Programming forums, part of the General Programming Boards category; The purpose of this problem is to simulate the tossing of two dice using an array of ints and the ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    Dice random generator problem

    The purpose of this problem is to simulate the tossing of two dice using an array of ints and the C++ random number generator rand(). When a pair of dice is tossed, the total number of spots showing on their faces are counted and this count is used as the index into an array consisting of 13 int elements. The random number generator will produce an int number from 0 to 12. A number which is either 0 or 1 is discarded. All other numbers are used as indexes into the array elements. The values of all array elements will be initialized to 0. Each indexed array element will be incremented by 1. The program will run until a total of 1,000 increments have been applied to array elements 2 through 12. Then, the program will display the counts in each of the array elements 2 through 12 and terminate.

    Any help I can get would be greatly appreciated, I am kinda new at this and to get off on the right foot would be a lot of help, thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    What have you tried?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    3

    re

    I tried this, but I think I am way off base:

    Code:
    #include <iostream> 
    #include <cstdlib> 
    #include <time.h> 
      
    using namespace std; 
      
    int rollDice(); 
      
    int main(int argc, char* argv) 
    { 
        int numRolls; 
        int arrRolls[12]; 
        int dieRoll; 
      
        srand((unsigned)time(0)); 
      
        for (int k = 0; k < 12; 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 < 12; j++) 
        { 
            cout << "The number " << j + 1 << " was rolled " << arrRolls[j] << " times." << endl; 
        } 
    } 
      
    int rollDice() 
    { 
        int result; 
      
        result = (rand() % 12) + 1; 
      
        return result; 
    }
    Last edited by cgalvani; 05-05-2010 at 10:17 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why are you asking how many times the dice are to be rolled? That's fixed in the problem statement. You're still allowing a roll of 1 to happen. The specs state that you need to have an array with 13 elements in it, and also that the dice roll itself must be used as the index element.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dice Simualtion
    By sillyman in forum C Programming
    Replies: 2
    Last Post: 04-22-2008, 01:43 PM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  4. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21