如何用不那么繁琐的方式统计一个随机数产生的次数?

How to count the number of time a random number is generated in a less cumbersome way?

如果一个程序应该模拟掷骰子,并掷骰子任意给定的次数。如何编写代码,以便它可以计算一个数字被滚动的次数?我在下面有一个代码,但我认为这是完成此任务的一种乏味且丑陋的方法。我认为有更好的方法可以做到这一点,也许是一个非常简单的代码。 这是代码:

#include <iostream>
#include <ctime>

int main(int argc, const char * argv[]) {
    int roll, s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0, i, roll_times = 0;
    std::cout << "How many times do you want to roll the dice?" << std::endl;
    std::cin >> roll_times;
    srand(unsigned(time(NULL)));
    for(i = 0; i < roll_times; i++) {
        roll = rand() % 6 + 1;
        switch(roll) {
            case 1:
                s1++;
                break;
            case 2:
                s2++;
                break;
            case 3:
                s3++;
                break;
            case 4:
                s4++;
                break;
            case 5:
                s5++;
                break;
            case 6:
                s6++;
                break;
        }
    }
    std::cout << "The number 1  was rolled " << s1 << " times." << std::endl;
    std::cout << "The number 2  was rolled " << s2 << " times." << std::endl;
    std::cout << "The number 3  was rolled " << s3 << " times." << std::endl;
    std::cout << "The number 4  was rolled " << s4 << " times." << std::endl;
    std::cout << "The number 5  was rolled " << s5 << " times." << std::endl;
    std::cout << "The number 6  was rolled " << s6 << " times." << std::endl;
    return 0;
}

为自己制作一个包含计数的 std::array。然后,每当出现数字时,您都会增加特定计数。例如:

std::array<unsigned, 6> numberCounts;

numberCounts.fill(0);

for (int i = 0; i < rollTimes; ++i)
{
    ++numberCounts[rollDice()];
}

// Print numberCounts

请注意:std::array 自 C++11 起可用。

我会使用数组,类似这样的东西:

 int counts[6] = {0};
 for(i=0;i<roll_times;i++) {
    counts[rand()%6]++;
  }

您可以使用包含 6 个整数的数组来代替单个变量。然后使用第一个 for 循环中的索引来设置计数。同样的想法可以应用到屏幕上的输出。

int main(int argc, const char * argv[]) {
    int roll, i, roll_times = 0;
    int dice[6] = {0};
    cout << "How many times do you want to roll the dice?" << endl;
    cin >> roll_times;
    srand(unsigned(time(NULL)));
    for(i = 0; i < roll_times; i++) 
    {
        roll = rand() % 6;
        dice[roll]++;
    }
    for(int k = 1; k <= 6; k++)
    {
        cout << "The number " << k << " was rolled " << dice[k-1] << " times." << endl;
    }
    return 0;
}

好的,我认为一个不那么繁琐的方法是创建一个大小为 6 的数组,然后每次滚动一个数字并等于该数组的索引号时加 1,但是因为数组索引在c++ 是从零开始的,我所要做的就是从滚动数中减去 1。例如,当程序滚动 1、5 次时,它被添加到数组 [滚动数 - 1] 或数组 [0]。所以我写了一个更好的代码:

#include <iostream>
#include <ctime>

int main(int argc, const char * argv[]) {
    int roll = 0, i = 0, roll_times = 0,roll_repeat[6] = {0};
    std::cout << "How many times do you want to roll the dice?" << std::endl;
    std::cin >> roll_times;
    srand(unsigned(time(NULL)));
    for(i = 0; i < roll_times; i++) {
        roll = rand() % 6 + 1;
        roll_repeat[roll - 1]++;
    }
    for(i = 0; i < 6; i++) {
        std::cout << "The number " << i + 1;
        std::cout << " was rolled " << roll_repeat[i] << " times.";
        std::cout << std::endl;
    }
    return 0;
}