C++ 掷骰子

C++ 掷骰子,c++,arrays,C++,Arrays,我需要这个程序做的是滚动36000 2d6,输出每个值的结果,以及它以表格形式出现的频率。不幸的是,我不熟悉数组的工作方式。这就是我到目前为止所做的: int DiceArray() { int rollOne = 0; int rollTwo = 0; int countrolls = 0; int sum = 0; for (countrolls=1; countrolls<=36000; countrolls++) {

我需要这个程序做的是滚动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;
    }
}
int数组()
{
int-rollOne=0;
int-rollTwo=0;
int countrolls=0;
整数和=0;

对于(countrolls=1;countrolls不确定您的要求,但似乎您需要一个简单的解决方案。如下所示:

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;
  }
}

不确定,你问的是什么,但似乎你需要一个简单的答案。比如:

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;
  }
}

你的
结果[11];
的想法是可行的。你也必须将其初始化为零

int result[11] = {0};
请记住,数组是以零为基础的。因此此数组将覆盖0-10的范围。您可以通过减去最小掷骰来处理此范围。增加循环中每个掷骰对应的数组位置:

++result[sum-2];
再次访问该值需要减去最小掷骰数:

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

你的
结果[11];
的想法是可行的。你也必须将其初始化为零

int result[11] = {0};
请记住,数组是以零为基础的。因此此数组将覆盖0-10的范围。您可以通过减去最小掷骰来处理此范围。增加循环中每个掷骰对应的数组位置:

++result[sum-2];
再次访问该值需要减去最小掷骰数:

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

像这样的方法应该会奏效:

#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;
}
#包括
#包括
#包括
std::array DiceArray(const std::size\u t count)
{
std::随机_装置;
std::mt19937发动机(设备());
标准:均匀分布(1,6);
std::数组结果={};
对于(std::size\u t i=0;istd::cout像这样的东西应该可以工作:

#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;
}
#包括
#包括
#包括
std::array DiceArray(const std::size\u t count)
{
std::随机_装置;
std::mt19937发动机(设备());
标准:均匀分布(1,6);
std::数组结果={};
对于(std::size\u t i=0;i这是一个C++11的答案

typedef std::mt19937 MyRNG;//带有常用参数选择的Mersenne捻线器
std::vector数组(
未签名的掷骰数,未签名的骰子数,
米尔恩&米尔恩
)
{
//d6!
标准:均匀分布d6(1,6);
std::vectorretval;
retval.resize(骰子数*6+1);
for(无符号计数=0;计数<转鼓数;++计数)
{
无符号和=0;
for(无符号i=0;i
接下来我们使用它:

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";
  }
}
intmain(intargc,char*argv[])
{
MyRNG-rng;
uint32\u t seed\u val=0;//以某种方式填充——作为'0',它每次都会复制相同的序列。一个常见的技巧是获取当前时间或其他熵源
rng.seed(seed_val);//避免在每次实验中多次调用它。它会重置rng,如果再次调用它,它将重复与第一次相同的数字序列。
标准::向量结果=数组(36000,2,rng);
for(无符号i=0;i这是一个C++11的答案

typedef std::mt19937 MyRNG;//带有常用参数选择的Mersenne捻线器
std::vector数组(
未签名的掷骰数,未签名的骰子数,
米尔恩&米尔恩
)
{
//d6!
标准:均匀分布d6(1,6);
std::vectorretval;
retval.resize(骰子数*6+1);
for(无符号计数=0;计数<转鼓数;++计数)
{
无符号和=0;
for(无符号i=0;i
接下来我们使用它:

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";
  }
}
intmain(intargc,char*argv[])
{
MyRNG-rng;
uint32\u t seed\u val=0;//以某种方式填充——作为'0',它每次都会复制相同的序列。一个常见的技巧是获取当前时间或其他熵源
rng.seed(seed_val);//避免在每次实验中多次调用它。它会重置rng,如果再次调用它,它将重复与第一次相同的数字序列。
标准::向量结果=数组(36000,2,rng);
for(无符号i=0;istd::cout声明数组将是一个良好的开端。:-)如果您有符合C++11标准的编译器,请不要使用rand():声明数组将是一个良好的开端。:-)如果您有符合C++11标准的编译器,请不要使用rand():@Vincent if
RAND_MAX
2^31-1
,通过
6
1
2
进行了20亿分之一的过度选择。可靠地检测到这一点将符合
10^18
试验的顺序(双重顺序不是打字错误,
10^18
是基于统计经验法则的餐巾纸估计值。)
rand()
成为一个糟糕的RNG的趋势会导致更多的不均匀性(这个词怎么拼写?--
rand()
通常是一个相当糟糕的RNG。@Vincent if
RAND_MAX
2^31-1
,通过
6
1
2
进行了20亿分之一的过度选择。可靠地检测到这一点将按照
10^18
试验的顺序进行(双重顺序不是打字错误,
10^18
是基于统计经验法则的餐巾纸估计值。)
rand()
成为不良RNG的趋势将导致