【问题标题】:Program to roll 2 dice 36000 times with possibilities程序以可能的方式滚动 2 个骰子 36000 次
【发布时间】:2015-07-19 05:49:05
【问题描述】:

我有一个 C++ 作业要做,这就是问题

//Dietel & Dietel C Programming //Chapter 6 Arrays: Page 241 Exercise:
6.19 /* 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 the two values should then be calculated. (Note: Since each die 
* can show an integer value from 1 to 6, then the sum of the two values will vary
* from 2 to 12 with 7 being the most freqent sum and 2 and 12 being the least frequent
* sums.) Figure 6.23 shows the 36 possible combinations of the two dice. 

* Your program should
* roll the two dice 36,000 times. Use a single-scripted array to tally the numbers of times 
* each possible sum appears. 

* Print the results in a tabular format. Also, determine if the totals 
* are resonable; i.e there are six ways to roll a 7, so approximately one sixth of all of the
* rolls should be 7. 
*/

我创建了程序,但它给了我这样的输出:

sum of Faces    Frequency
      2            0
      3         4041
      4            0
      5         7922
      6            0
      7        12154
      8            0
      9         7936
     10            0
     11         3948
     12            0
sum: 36001

我不明白为什么它给所有偶数的频率都设为 0

这是我目前编写的代码:

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

int main()
{

    const int arraysize = 13;

    int counter[13], sum=0;
    // init counter
    for(int i=0; i<13; i++)
        counter[i] = 0;

    int die1; 
    int die2;

    for ( int roll1 = 0; roll1 <=36000; roll1++ ) {
        die1 =  1 + rand() % 6;
        die2 =  1 + rand() % 6;
        counter[die1+die2]++;
    }

    cout<<"sum of Faces"<<setw(13)<<"Frequency"<<endl;

    for(int face=2; face<arraysize;face++)
    {
        cout<<setw(7)<<face<<setw(13)<<counter[face]<<endl;
        sum += counter[face];
    }

    cout << "sum: " << sum;


    return 0;
}

我还需要为骰子添加可能性,例如:

1 + 1 = 2 : 1 possibility for sum to be 2
1 + 2 = 2 + 1 = 3 : 2 possibility for sum to be 3
1 + 3 = 2 + 2 = 3 + 1 = 4 : 3 possibility for sum to be 4
.
.
.
6 + 6 = 12 : 1 possibility for sum to be 12

【问题讨论】:

  • 又来了一个作业,让我们这样做吧!! :D
  • I don't why it's giving 0 frequency for all even numbers 为什么不呢?你写程序了吗?如果您这样做了,请调试您的程序以找出偶数不正确运行的原因。
  • 看起来您对rand 的实现可能很差。尝试改用现代的&lt;random&gt; 库。
  • 如果您没有复制并粘贴该代码而是重新输入了它,我会寻找一个错字,其中die1 意外变为die2,反之亦然。
  • @moose 你可以很容易地成为你自己的侦探。只需通过添加两个简单的cout 行(最基本的调试技术——输出值)来输出您正在使用的值。如果您只是简单地输出die1die2 的值,那么您会将问题集中在rand() 函数无法正常工作上。那么你的问题可能是my rand() function is not working correctly when I do this... 而不是I don't know what's wrong with my program

标签: c++ arrays dice


【解决方案1】:

我只是复制你的代码并运行它,你似乎缺少 rand 函数的库(不知道你是如何运行你的)反正我只是为 rand 导入库....

#include <bits/stdc++.h>
using namespace std;

int main()
{

    const int arraysize = 13;

    int counter[13], sum=0;
    // init counter
    for(int i=0; i<13; i++)
        counter[i] = 0;

    int die1; 
    int die2;

    for ( int roll1 = 0; roll1 <=36000; roll1++ ) {
        die1 =  1 + rand() % 6;
        die2 =  1 + rand() % 6;
        counter[die1+die2]++;
    }

    cout<<"sum of Faces"<<setw(13)<<"Frequency"<<endl;

    for(int face=2; face<arraysize;face++)
    {
        cout<<setw(7)<<face<<setw(13)<<counter[face]<<endl;
        sum += counter[face];
    }

    cout << "sum: " << sum;


    return 0;
}

它在我的机器上运行正常。

【讨论】:

  • 这是完全相同的代码,但带有非标准标题。
  • @yahyaelfakir 傻我!在 Visual Studio 上试过,效果很好。给出每个面的所有频率。但我在想如何添加这样的可能性: 1 + 1 = 2 : 1 总和为 2 的可能性 1 + 2 = 2 + 1 = 3 : 2 总和为 3 的可能性.....我是认为我需要为此添加另一个 coutner/for?
  • 嗯,这是另一个问题...是的,您可以使用从 1 到 6 的 2 个循环并将总和存储在大小为 12 的数组中。
【解决方案2】:

您似乎没有播种随机函数,这会产生奇怪的结果

解决方案:

#include <ctime>   // time()
#include <cstdlib> // srand(), rand()

...

srand(time(NULL)); // There are better ways to seed rand(), but this is simple

...

for ( int roll1 = 0; roll1 <=36000; roll1++ ) {
    die1 =  1 + rand() % 6;
    die2 =  1 + rand() % 6;
    counter[die1+die2]++;
}

另外,如果您想要一个更像 C++ 的解决方案(并且可以访问 C++11),check this out

【讨论】:

  • 不播种不会产生“奇怪”的结果;它只会为每个程序运行提供相同的结果。如果实现被严重破坏以至于默认种子永远不会给出两个连续的偶数,那么播种将无济于事。
  • @levi 我们不允许使用 ctime 或 cstdlib。你有一个不涉及额外库的解决方案吗?还有那些可能性呢?
  • @moose 您正在使用的 rand() 实现已损坏,而不是您的程序。
  • @moose:如果你不能使用&lt;cstdlib&gt;,那么你就不能使用rand()(当你包含其他库头时,你的程序一定是不小心包含了它)。如果您应该编写自己的伪随机数生成器,那么 google 会很快告诉您如何操作。
【解决方案3】:

让我们玩骰子吧!传统模具是立方体。它的六个面中的每一个都显示了从 1 到 6 的不同数量的点。骰子用于产生从 1 到 6 的结果。当掷骰子(或滚动)并且骰子静止时,骰子的面是uppermost 提供了 throw 的值。如果掷出一个无偏的骰子,则从 1 到 6 的每个值都有相同的可能性。

您的编程任务与分析掷骰子产生的序列有关。

创建一个名为 Dice 的程序来解决以下练习。程序的输入是所谓的试验,即滚动结果的序列。此输入必须从标准输入中读取。输入的第一行包含一个整数 N,表示抛出次数 \left(1\le N:\le1000000\right)。输入的第二行正好包含 N 个字符,每个字符是从 1 到 6 的一个数字。例如,输入可以如下:

8 32646135 程序的输出应该写入标准输出。您需要解决 3 个练习。输出应该正好包含 3 行:输出中的第 i 行是练习 i 的解。


练习 练习 1 在试验中发生了多少次,恰好两个 6 依次滚动?例如,在序列 56611166626634416 中,它出现了两次,恰好两个 6 相继抛出。

练习 2 找出连续滚动的最长子序列的长度,其中不出现值 6。 (如果只扔了 6s,这个数字可以为零。)例如,在试验 66423612345654 中,连续掷骰中没有出现值 6 的最长子序列是 12345。它的长度是 5。

练习 3 如果该序列仅包含 5s 和 6s,我们将把试验中的连续掷骰序列称为幸运序列。例如 6556665 是一个幸运系列,长度为 7。 找出,这是幸运系列最常见的长度。如果有多个“最频繁”的幸运系列长度,则打印最长的。如果试用中没有幸运系列,则打印零。

注意。我们对最常见的幸运系列不感兴趣。 656、555、556、666这四个幸运系列对我们来说是等价的,都是长度为三的幸运系列。我们正在寻找最常见的幸运系列长度。

例如,在试用 5533661656 中,656 系列是最长的幸运系列。但审判中只有一个幸运系列,长度为三。 55和66也是幸运系列。这就是为什么正确答案是 2。在试验 456116513656124566 中,长度为 2 和 3 的幸运系列都出现了两次,它们之间存在平局。现在应该打印最长的长度(即 3)。示例example1和example2旨在说明这种情况。


示例 我们提供三个简单的例子来阐明练习。也可以使用其他更大的示例来测试您的解决方案!

示例 1 示例输入:

9 616161666 示例输出:

0 1 1 示例 2 示例输入:

18 456116513656124566 示例输出:

1 4 3 示例 3 示例输入:

17 56611166626634416 示例输出:

2 4 3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 2013-05-09
    • 2014-03-16
    • 2013-09-17
    • 1970-01-01
    相关资源
    最近更新 更多