c++randomprobabilitysimulatedice

A Program for Simulating the Rolling of 2 Dice


The following is the whole question.

Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. The sum of two values should then be calculated. [Note : Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.] Note that there are 36 possible combinations of the two dice. Your program should roll the two dice 3,600 times. Use a one_dimensional array to tally the numbers of times each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable (i.e., there are six ways to roll a 7, so approximately one-sixth of all the rolls should be 7).

The result should be the following :

Question 2
Please enter the seed : 2

enter image description here

I don't know how to generate the "expected" column. enter image description here

Here is my program : (The main is Q2_main())

#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

double total_Array[11];
double expected_Array[11];
double actual_Array[11];
int seed;

void initialization_of_Array()
{
    for (int counter=0; counter < 12; counter++)
    {
        total_Array[counter] = 0;
        expected_Array[counter] = 0;
        actual_Array[counter] = 0;
    }
}

void show_heading_line()
{
    cout << setw(5) << "Sum" 
        << setw(10) << "Total" 
        << setw(17) << "Expected" 
        << setw(16) << "Actual" 
        << endl;
}

void show_Data_Results_line(int sum, int total, double expected, double actual)
{
    cout << setw(5) << sum
        << setw(10) << total
        << setw(16) << expected << "%"
        << setw(15) << actual << "%"
        << endl;
}

void calculation_of_total()
{
    int die_1, die_2;
    for (int counter = 1; counter <= 3600; counter++)
    {
        die_1 = 1 + rand() % 6;
        die_2 = 1 + rand() % 6;
        total_Array[((die_1 + die_2)-2)]++;
    }
}

void calculation_of_expect()
{

}

void calculation_of_actual()
{
    for (int counter = 0; counter < 11; counter++)
    {
        actual_Array[counter] = (total_Array[counter] / 3600.0) * 100.0;
    }
}

void rollDice_Operation()
{
    calculation_of_total();
    calculation_of_expect();
    calculation_of_actual();
}

void print_Result()
{
    show_heading_line();
    for (int counter = 0; counter <= 10; counter++)
    {
        show_Data_Results_line((counter+2), total_Array[counter], 1, actual_Array[counter]);
    }
}

void Q2_main()
{
    cout << setprecision(3) << fixed;
    initialization_of_Array();
    cout << "Please enter the seed : ";
    cin >> seed;
    srand(seed);
    rollDice_Operation();
    print_Result();
}

Anyone can give me some hints to deal with the "expected" column?


Thank you for your attention


Solution

  • The expected column is just the mathematical probability of the result:

    +-------+-------------------------+--------------------+-------------+
    | Value |      Possibilities      | # of possibilities | Probability |
    +-------+-------------------------+--------------------+-------------+
    |     2 | 1+1                     |                  1 | 1/36=2.78%  |
    |     3 | 1+2,2+1                 |                  2 | 2/36=5.56%  |
    |     4 | 1+2,2+2,2+1             |                  3 | 3/36=8.33%  |
    |     5 | 1+4,2+3,3+2,4+1         |                  4 | 4/36=11.11% |
    |     6 | 1+5,2+4,3+3,4+2,5+1     |                  5 | 5/36=13.89% |
    |     7 | 1+6,2+5,3+4,4+3,5+2,6+1 |                  6 | 6/36=16.67% |
    |     8 | 2+6,3+5,4+4,5+3,6+2     |                  5 | 5/36=13.89% |
    |     9 | 3+6,4+5,5+4,6+3         |                  4 | 4/36=11.11% |
    |    10 | 4+6,5+5,6+4             |                  3 | 3/36=8.33%  |
    |    11 | 5+6,6+5                 |                  2 | 2/36=5.56%  |
    |    12 | 6+6                     |                  1 | 1/36=2.78%  |
    +-------+-------------------------+--------------------+-------------+
    

    You don't have to compute it, just print it in order to compare with the actual statistical results:

    double expected_Array[11] = {1/.36, 2/.36, 3/.36, 4/.36, 5/.36, 6/.36, 5/.36, 4/.36, 3/.36, 2/.36, 1/.36};
    ...
    show_Data_Results_line((counter+2), total_Array[counter], expected_Array[counter], actual_Array[counter]);