質問

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

役に立ちましたか?

解決

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]);

他のヒント

The expected column should contain the probability that a roll of a dice ends up with the given sum. This is pure maths probability theory to be more specific, but you can also brute force its computation. Compute all the possible rolls of the dice and for each roll increase the number of rolls that result in the given sum. After that the expected value for each sum is equal to the number of ways in which you can get that sum divided by the total number of possible rolls(how many different rolls are possible with 2 dice?).

      1   2   3   4   5   6
    +---+---+---+---+---+---+
  1 | 2 | 3 | 4 | 5 | 6 | 7 |
    +---+---+---+---+---+---+
  2 | 3 | 4 | 5 | 6 | 7 | 8 |
    +---+---+---+---+---+---+
  3 | 4 | 5 | 6 | 7 | 8 | 9 |
    +---+---+---+---+---+---+
  4 | 5 | 6 | 7 | 8 | 9 | 10|
    +---+---+---+---+---+---+
  5 | 6 | 7 | 8 | 9 | 10| 11|
    +---+---+---+---+---+---+
  6 | 7 | 8 | 9 | 10| 11| 12|
    +---+---+---+---+---+---+

so to calculate the expected probability of 9 it is the number of combinations in the table above that becomes 9 divided by total 36 i.e. 4/36

I know this answer is eight years late but here it is...

int nExpectedOutcome(int x, int y) {
    return (x - abs(y-(x+1))) * 100000; 
}

void calculation_of_expect() {
    int numDice = 2; 
    int dFaces = 6; 
    int RT = 36000; 
    int expectedOutcome = (dFaces * numDice) - numDice;
    
    for (int i = 0; i <= expectedOutcome; i++) {
        expected_Array[i] = nExpectedOutcome(dFaces, i+2) / (float)RT;
    }
}

...

show_Data_Results_line(
    counter+2,
    total_Array[counter],
    expected_Array[counter],
    actual_Array[counter]
);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top