如何以较不繁琐的方式计算随机数生成的时间?

时间:2015-01-15 18:23:37

标签: c++ random

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

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

4 个答案:

答案 0 :(得分:1)

让自己成为包含计数的std::array。然后,只要出现数字,就会增加特定计数。例如:

std::array<unsigned, 6> numberCounts;

numberCounts.fill(0);

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

// Print numberCounts

请注意:自{+ C ++ 11>以来std::array可用。

答案 1 :(得分:1)

我会使用一个类似的数组:

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

答案 2 :(得分:1)

您可以使用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;
}

答案 3 :(得分: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;
}