使用RAND()掷硬币和掷骰子时, [英] Using rand() when flipping a coin and rolling a die

查看:119
本文介绍了使用RAND()掷硬币和掷骰子时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我完全糊涂了。因此,有这个练习本课程中,我阅读,我有一个很难知道这个东西,因为我被我自己学C。这里的练习:

I'm totally confused now. So there's this exercise in this lesson I'm reading and I'm having a hard time knowing this stuff since I'm learning C by myself. Here's the exercise:

说明:

编写一个程序,将模拟以下随机
    使用兰特事件() C标准函数
    库。我们感兴趣的是确定兰特()
    似乎真的产生一个随机值的集合。去做这个
    我们将重复所述事件的一定的次数并看到
    我们是如何经常​​得到每个可能的结果。如果兰特()
    是真正随机的,我们对可能的结果映射
    统一,我们的结果应该是等可能的,因此
    在所有可能的结果分布均匀。

Write a program that will simulate the following random events using the rand() function in the C standard libraries. We are interested in determining if rand() really seems to produce a random set of values. To do this we will repeat the event a certain number of times and see how often we get each of the possible results. If rand() is truly random, and our mapping to the possible results is uniform, our results should be equally likely and therefore evenly distributed across all possible results.

这两个事件将是:


      
  1. 抛硬币

  2.   
  3. 滚动一个六面骰子

  4.   

重复每个事件:


      
  1. 10

  2.   
  3. 100

  4.   
  5. 1000

  6.   
  7. 10,000

  8.   
  9. 10万次

  10.   

在每组重复的,怎么算往往每个结果
    发生。然后计算总的事件的百分比
    被重新$ P $每个结果psented。

Within each set of repetitions, count how often each result occurs. Then compute the percentage of the total events were represented by each result.

例如:

Coin:
----
  Flips: 10       Heads: 40%
                  Tails: 60%
Die:
----
  Rolls: 10       1: 10%
                  2:  0%
                  3: 20%
                  4: 30%
                  5: 30%
                  6: 10%

          ...

Coin:
----
  Flips: 100000   Heads: 49%
                  Tails: 51%

Die:
----
  Rolls: 100000   1: 17%
                  2: 18%
                  3: 17%
                  4: 17%
                  5: 17%
                  6: 16%

目前我有:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, value, n;
    int flips[2] = {0};
    int rolls[6] = {0};

    for(n = 10; n <= 100000; n *= 10){
       for(i=0; i < n; i++){
       value = rand( )%2;
       flips[value]++;

       value = rand( )%6;
       rolls[value]++;
       }

       printf("Coin:\n____\n  Flips: %d\n",n);
       printf("    Heads: %d%%\n", ((flips[0] * 100) / n));
       printf("    Tails: %d%%\n\n", ((flips[1] * 100) / n));

       printf("Die:\n____\n  Rolls: %d\n",n);
       printf("    1: %d%%\n", rolls[0]);
       printf("    2: %d%%\n", rolls[1]);
       printf("    3: %d%%\n", rolls[2]);
       printf("    4: %d%%\n", rolls[3]);
       printf("    5: %d%%\n", rolls[4]);
       printf("    6: %d%%\n", rolls[5]);
    }

    return 0;
}

*对不起,我真的不专注于芯片的输出,只是试图让硬币到工作第一

*Sorry I'm not really focusing on the die outputs, just trying to get coins to work first

推荐答案

我把你的code和应用我建议的变化,还显示如何计算的百分比。

I took your code and applied my suggested changes, and also show how to compute the percentages.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, j, value, n;
    static const char *sides[] = {"Heads", "Tails"};
    int flips[2] = {0};
    int rolls[6] = {0};

    i = 0;

    for(n = 10; n <= 100000; n *= 10){
       for(; i < n; i++){
         value = rand( )%2;
         flips[value]++;

         value = rand( )%6;
         rolls[value]++;
       }

       printf("Coin:\n____\n  Flips: %d\n",i);
       for(j = 0; j < 2; j++) {
           printf("    %s: %d%%\n", sides[j], 100*flips[j]/i);
       }

       printf("Die:\n____\n  Rolls: %d\n",i);
       for(j = 0; j < 6; j++) {
           printf("    %d: %d%%\n", j, 100*rolls[j]/i);
       }
    }
    return 0;
}

这篇关于使用RAND()掷硬币和掷骰子时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆