2014-03-05 66 views
1

我需要这个程序要做的是滚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}

但我怎么输出我的总和阵列?

+0

声明数组,将是一个良好的开端。 :-) –

+1

如果你有一个C++ 11兼容的编译器,请不要使用rand():http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3924.pdf – Vincent

回答

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

如果RAND_MAX不能被6整除,那么你的分布是不均匀的... – Vincent

+1

@Vincent如果'RAND_MAX'是'2^31-1',它有助于过度选择20亿分之1到'1' '通过'6'。如果能够可靠地进行检测,就会按照“10^18”试验的顺序进行(两次排序不是错字--10^18是基于统计经验的餐巾纸估计)。 rand()是一个糟糕的RNG的趋势将会为非均匀性贡献更多**(你怎样拼写这个单词?) - rand()通常是一个非常糟糕的RNG。 – Yakk

0

你的result[11];想法是可行的。你也必须将它初始化。

int result[11] = {0}; 

请记住,数组是从零开始的。所以这个数组将覆盖0-10的范围。你可以通过减去最小的掷骰子来处理。增加相应的阵列位置在循环的每个卷:

++result[sum-2]; 

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

int numTwos = result[2-2]; 
int numTens = result[10-2]; 
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; 
} 
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"; 
    } 
}