Search code examples
c++dice

Roll 2 dice 1000 times


My professor asked us to write a program that:

  1. uses a loop to simulate the rolling of a pair of dice one thousand times(Here I think a for loop would be useful).

  2. With each iteration, the loop needs to count the number of times each value from 2 to 12(Here I am thinking if/else statements would apply)

  3. When the loop ends, must display the number of times each value(from 2 to 12) occurred.

He has the assignment structured like so:

He wants us to use a function that goes into the 1000-time for loop, that calls ANOTHER function TWO TIMES per function call(to simulate two dice being thrown).

Let me explain what I have managed to put down

//
//  main.cpp
//  RollingDice

#include <iostream>
#include <ctime>
using namespace std;
int roll();
int rollDice();

int main(int argc, const char * argv[])
{

    for (int i = 1; i < 1000; i++)
    {

        rollDice(); //This is the function that is supposed to call the roll(); 
                    //function two times. This makes sense to me that TWO DICE 
                    //are being rolled 1000 times. 

    }

    int result; //These two statements was where I was just hoping for something 
                //to work. I put these variable statements outside of the for 
                //loop because I was thinking that the int rollDice(); function 
                //definition(below) wouldn't know how to use it otherwise. I 
                //know that doesn't make sense, but I just can't explain why. 

    result = rollDice();

}

int roll()
{ //This function was provided to us by my professor. 
    static bool randomInitialized = false;

    int points;

    if (!randomInitialized)
    {
        srand((unsigned int) time(NULL));
        randomInitialized = true;

    }
    points = (rand() % 6) + 1;
    return points;
}

int rollDice()
{ //This I wrote myself. I'm imagining this is how you call a function twice. 
  //The return statement in this function was my attempt of returning the sum
  //of the values of the two dice.
    roll();
    roll();

    return result;
}

Besides this part of the program not working, the other issue I still have is determining a way to have a counter for each value that occurs(however, I am imagining that that part of the program belongs in the for loop. that's about all I know though.). I have thinking deeply about this program since yesterday. I came back to it today hoping a fresh mind would solve it, but I'm still struggling. Any and all help is greatly appreciated.


Solution

  • The expression roll() evaluates to a number. To add numbers, we use +. To return a value, we use return.

    Putting that together, we get a simple function to sum two rolls

    int rollDice() { return roll() + roll(); }
    

    If you have a numbered sequence of things, and the numbers are both close together and start near 0, one of the standard library's SequenceContainers is an appropriate holder for the whole sequence.

    Here the things are counts for a particular throw. We know up front exactly the available values (2 - 12 inclusive), so std::array is appropriate. Any integral value that can hold at least 1000 is appropriate for a count. I choose std::size_t here.

    #include <array>
    #include <iostream>
    
    int main()
    {
        std::array<std::size_t, 13> counts {}; 
    

    This will give us 13 0s, starting at position 0

        for (std::size_t i = 0; i < 1000; ++i)
        {
             ++counts[rollDice()]; 
    

    We choose which number with rollDice, and use it to select a count to increment

        }
    
        for (std::size_t i = 2; i < 13; ++i)
        {
    

    We can now loop over our results, displaying the counts

             std::cout << "The count for " << i << " is " << counts[i] << std::endl;
        }
    }