【问题标题】:A Program for Simulating the Rolling of 2 Dice一个模拟掷两个骰子的程序
【发布时间】:2013-11-28 17:57:20
【问题描述】:

以下是整个问题。

编写一个模拟掷两个骰子的程序。该程序 应该使用 rand 来滚动第一个骰子,并且应该再次使用 rand 掷第二个骰子。然后应计算两个值的总和。 [注:每个骰子可以显示一个从 1 到 6 的整数值,所以总和 这两个值将在 2 到 12 之间变化,其中 7 是最常见的 sum 和 2 和 12 是最不频繁的和。] 请注意,有 两个骰子的 36 种可能组合。你的程序应该滚动 两个骰子 3,600 次。使用一维数组来计算数字 每个可能的总和出现的次数。以表格形式打印结果 格式。此外,确定总数是否合理(即,有 掷 7 的六种方法,因此大约是所有掷骰的六分之一 应该是 7)。

结果应该如下:

Question 2
Please enter the seed : 2

我不知道如何生成“预期”列。

这是我的程序:(主要是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();
}

谁能给我一些处理“预期”列的提示?


感谢您的关注

【问题讨论】:

  • 这是一个编程问题还是您无法理解这个问题?
  • expected result 不是计算结果,它是特定于问题的数学结果(两个骰子的总和)。您不必为该列计算任何内容。
  • 我想我无法理解这个问题......
  • 当有人在 c++ 中使用 rand 时一如既往:rand is considered harmful

标签: c++ random probability simulate dice


【解决方案1】:

预期的列只是结果的数学概率:

+-------+-------------------------+--------------------+-------------+
| 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%  |
+-------+-------------------------+--------------------+-------------+

您不必计算它,只需打印它以便与实际统计结果进行比较:

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

【讨论】:

    【解决方案2】:

    预期列应包含掷骰子最终得到给定总和的概率。这是更具体的纯数学概率论,但您也可以蛮力计算。计算所有可能的骰子掷骰数,并为每次掷骰增加掷骰数,从而得出给定的总和。之后,每个总和的期望值等于获得该总和的方法数除以可能的掷骰总数(2 个骰子可能掷多少不同的骰子?)。

    【讨论】:

      【解决方案3】:
            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 计算 9 的期望概率 就是上表中的组合数 变成 9 除以总数 36 即 4/36

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-26
        • 1970-01-01
        • 2011-07-12
        • 2021-12-04
        • 2018-01-03
        • 2019-03-09
        • 1970-01-01
        相关资源
        最近更新 更多