【问题标题】:Roll 5 dice and see if 3 are the same [closed]掷5个骰子,看看3个是否相同[关闭]
【发布时间】:2017-03-24 00:30:44
【问题描述】:

完整问题:模拟重复试验以估计掷五个骰子并获得至少三个相同数字的概率。

我知道如何通过反复试验和其他东西来检查概率。我只是不知道如何看五个骰子中的三个是否相同。需要注意的重要一点:我们还没有在课堂上学习数组,所以我不能使用它。我们已经学习了对象、决策,并且目前正在学习循环。任何有关如何检查三个是否相同的帮助将不胜感激。这是我现在拥有的:

Random rand = new Random();

    final int TRIALS = 100_000;
    int same = 0;//check if they are the same??

    for(int i=0; i<TRIALS; i++){
        int a = rand.nextInt(6)+1;
        int b = rand.nextInt(6)+1;
        int c = rand.nextInt(6)+1;
        int d = rand.nextInt(6)+1;
        int e = rand.nextInt(6)+1;

    }

【问题讨论】:

  • 你想要概率还是检查实际上是否是3个相同的数字并返回true/false
  • @Thrasher 评论的拍摄结束。无需实际上掷骰子即可找到概率,因此我等待对评论的回复。

标签: java loops random dice


【解决方案1】:

我想这应该适合你:

        Random rand = new Random();

    final int TRIALS = 100;
    int same = 0;//check if they are the same??
    int aux = 0;
    for(int i=0; i<TRIALS; i++){
        aux = 0;
        int a = rand.nextInt(6)+1;
        int b = rand.nextInt(6)+1;
        int c = rand.nextInt(6)+1;
        int d = rand.nextInt(6)+1;
        int e = rand.nextInt(6)+1;
        if(a == b || a == c || a == d || a == e)
            aux++;
        if(b == c || b == d || b == e)
            aux++;
        if(c == d || c == e)
            aux++;
        if(d == e)
            aux++;
        if(aux > 2)
            same++;
    }
    System.out.println("The posibilities are: " + same + "/" + TRIALS);
}

尽量不要使用你现在所知道的之外的任何东西。

你也可以试试这个,是一样的,但你会有确切的问题:

    final int TRIALS = 100;
    int same = 0;//check if they are the same??
    int aux = 0;
    for(int i=0; i<TRIALS; i++){
        aux = 0;
        int a = rand.nextInt(6)+1;
        int b = rand.nextInt(6)+1;
        int c = rand.nextInt(6)+1;
        int d = rand.nextInt(6)+1;
        int e = rand.nextInt(6)+1;
        if(a == b || a == c || a == d || a == e)
            aux++;
        if(b == c || b == d || b == e)
            aux++;
        if(c == d || c == e)
            aux++;
        if(d == e)
            aux++;
        if(aux > 2)
            same++;
    }
    double prob = same/TRIALS;
    System.out.println("The posibilities are: " + prob);
}

【讨论】:

    【解决方案2】:

    所以我在这里所做的只是模拟 1000 次并在每次模拟中掷 5 个骰子,并通过递增表示该结果的整数变量(即一、二等)来记录每个结果,最后,如果任何整数> 2(即在 5 次中出现 3 次以上),那么我将增加相同的值。然后你可以把它除以 1000 得到概率。

    Random rand = new Random();
    
    final int TRIALS = 1000;
    int same = 0;//check if they are the same??
    
    for(int i = 0; i < TRIALS; i++){
        int ones = 0;
        int twos = 0;
        int threes = 0;
        int fours = 0;
        int fives = 0;
        int sixes = 0;
        for (int j = 0; j < 5; j++) {
            int curr = rand.nextInt(6) + 1;
            if (curr == 1) {
                ones++;  
            } else if (curr == 2) {
                twos++; 
            } else if (curr == 3) {
                threes++;
            } else if (curr == 4) {
                fours++;
            } else if (curr == 5) {
                fives++;
            } else if (curr == 6) {
                sixes++;
            }
        }
        if (ones > 2 || twos >= 2 || threes >= 2 || fours > 2 || fives > 2 || sixes > 2) {
            same++;
        }
    }
    
    double probability = (double)same/TRIALS;
    

    【讨论】:

      【解决方案3】:

      场景:

      骰子 五次 特定次数,并计算得到至少 3 个数字等于(包括 4 和 5)的估计概率。

      流程图:

      我创建了一个流程图,以便您了解正在计算的内容。

      代码:

      import java.util.Random;
      
      public class RollDices
      {
          public static void main ( String [ ] args )
          {
              Random rand = new Random();
      
              int threeEquals=0;
              int notThreeEquals=0;
              final int TRIALS = 567;
      
      
              int dice = 0; 
      
              int one = 0;
              int two = 0;
              int three = 0;
              int four = 0;
              int five = 0;
              int six = 0;
      
              for(int i=0; i<TRIALS; i++)
              {
                  for(int j = 0; j < 5; j++)
                  {
      
                      dice = rand.nextInt(6)+1;
                      if(dice == 1) one++;
                      if(dice == 2) two++;
                      if(dice == 3) three++;
                      if(dice == 4) four++;
                      if(dice == 5) five++;
                      if(dice == 6) six++;
      
                  }
      
      
                  if( one >= 3 ||
                          two >= 3 ||
                          three >= 3 ||
                          four >= 3 ||
                          five >= 3 ||
                          six >= 3)
                  {
                      System.out.println ( "Yay! one number is repeated at least 3 times" );
                      System.out.println ( "[" + one + "," + two +","+ three +","+ four +","+ five +","+ six +"]");
                      threeEquals++;
                  }
                  else
                  {
                      System.out.println ( "No number is repeated 3 times" );
                      System.out.println ( "[" + one + "," + two +","+ three +","+ four +","+ five +","+ six +"]");
                      notThreeEquals++;
                  }
      
      
                 one = 0;
                 two = 0;
                 three = 0;
                 four = 0;
                 five = 0;
                 six = 0;
              }
              System.out.println ( "At least three equals count: " + threeEquals );
              System.out.println ( "Less than three equals count: " + notThreeEquals );
              System.out.println ( "Trials: " + TRIALS );
              System.out.println ( "Estimated probability: " + ((double)threeEquals/TRIALS)*100 + "%");
      
          }
      }
      

      输出:

      没有数字重复3次

      ... [0,0,1,2,1,1]

      没有数字重复3次

      [1,0,1,0,1,2]

      耶!一个数字至少重复 3 次

      [0,1,0,0,3,1]

      耶!一个数字至少重复 3 次

      [0,0,2,3,0,0]

      至少三个等于计数:119

      小于三等于计数:448

      试验:567

      估计概率:20.98765432098765%


      额外:

      您无需掷骰子即可计算概率。你需要问“它们都不发生的概率是多少?”并从 1 中减去(因为“没有发生”的补充事件是“至少发生了一次”)。

      我检查了 5 个掷骰子中的所有可能性,并计算其中有 3 个或更多重复数字,结果是:

      3 的概率等于:

      3不等于的概率:

      综上,最终公式为:

      示例:

      用上面的公式:

      如您所见,每掷 5 次骰子,您获得至少三个相等数字的概率就会大大增加。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-19
        • 2013-06-12
        • 2013-10-30
        • 1970-01-01
        • 2010-11-15
        • 2015-06-29
        • 2018-12-17
        相关资源
        最近更新 更多