滚动骰子的阵列

时间:2014-03-05 19:10:35

标签: c++ arrays

我需要这个程序做的是滚动36000 2d6,输出每个值的结果以及它以表格格式出现的频率。不幸的是,我不熟悉数组的工作原理。这就是我到目前为止所做的:

int DiceArray()
{
    int rollOne = 0;
    int rollTwo = 0;
    int countrolls = 0;
    int sum = 0;
    for (countrolls=1; countrolls<=36000; countrolls++)
    {
        rollOne = (rand() % 6) + 1;
        rollTwo = (rand() % 6) + 1;
        sum = rollOne+rollTwo;
    }
}

所以,我需要一个用于骰子结果的数组,我猜测它会看起来像结果[11],因为它列出了2到12的骰子总和。然后我将不得不使数组成为多维的;我需要第二列结果。

因此,例如,两次的结果将会发生,我们会说700次。所以我需要像结果那样的东西[2]。是对的吗?无论如何,我如何为我的阵列获得正确的值?

我想结果数组我只是这样列出它们因为它们总是相同的:{2,3,4,... 12}

但是如何将我的总和输出到数组?

4 个答案:

答案 0 :(得分:3)

不确定,你在问什么,但似乎你需要一个简单的histogram。像这样:

void DiceArray()
{
  int rollOne = 0;
  int rollTwo = 0;
  int sum = 0;

  // This array holds histogram. hist[0] and hist[1] are always zero.
  int hist[13] = { 0 }; 

  for (int countrolls = 0; countrolls < 36000; ++countrolls)
  {
    rollOne = (rand() % 6) + 1;
    rollTwo = (rand() % 6) + 1;
    sum = rollOne+rollTwo;
    hist[sum]++;
  }

  for (int i = 2; i <= 12; ++i)
  {
    std::cout << i << ": " << hist[i] << std::endl;
  }
}

此功能打印以下内容:

2: 949
3: 1974
4: 2898
5: 3987
6: 5133
7: 6088
8: 4944
9: 3976
10: 3075
11: 1991
12: 985

答案 1 :(得分:1)

这样的事情应该有效:

#include <iostream>
#include <random>
#include <array>

std::array<std::size_t, 13> DiceArray(const std::size_t count)
{
    std::random_device device;
    std::mt19937 engine(device());
    std::uniform_int_distribution<std::size_t> distribution(1, 6);
    std::array<std::size_t, 13> result = {};
    for (std::size_t i = 0; i < count; ++i) {
        ++result[distribution(engine)+distribution(engine)];
    }
    return result;
}

int main(int argc, char* argv[])
{
   auto result = DiceArray(36000);
   for (std::size_t i = 0; i < result.size(); ++i) {
       std::cout<<i<<" "<<result[i]<<std::endl;
   }
   return 0;
}

答案 2 :(得分:0)

您对result[11];的想法会奏效。你也必须对它进行零初始化。

int result[11] = {0};

请记住,数组是从零开始的。所以这个数组将涵盖0-10的范围。您可以通过减去最小骰子滚动来解决这个问题。增加循环中每个卷的相应数组位置:

++result[sum-2];

再次访问该值需要减去最小骰子:

int numTwos = result[2-2];
int numTens = result[10-2];

答案 3 :(得分:0)

这是C ++ 11的答案。基于this stack overflow answer

 typedef std::mt19937 MyRNG;  // the Mersenne Twister with a popular choice of parameters

 std::vector< unsigned > DiceArray(
   unsigned how_many_rolls, unsigned dice_count,
   MyRNG& rng
 )
 {
   // d6!
   std::uniform_int_distribution<uint32_t> d6(1,6);
   std::vector< unsigned > retval;
   retval.resize( dice_count * 6+1 );
   for (unsigned count = 0; count < how_many_rolls; ++count)
   {
     unsigned sum = 0;
     for(unsigned i = 0; i < dice_count; ++i) {
       sum += d6(rng);
     }
     retval[sum] += 1;
   }
   return retval;
 }

接下来我们使用它:

int main(int argc, char* argv[])
{
  MyRNG rng;
  uint32_t seed_val = 0;       // populate somehow -- as `0` it will replicate the same sequence each time.  A common trick is to grab the current time or some other source of entropy
  rng.seed(seed_val); // avoid calling this more than once per experiment.  It resets the RNG, if you call it again it will repeat the same sequence of numbers as the first time.
  std::vector<unsigned> result = DiceArray(36000, 2, rng);
  for (unsigned i = 0; i < result.size(); ++i) {
     std::cout << i <<": " << result[i] << "\n";
  }
}