滚动5个骰子,看看3是否相同

时间:2016-11-10 04:07:24

标签: java loops random dice

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

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

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 个答案:

答案 0 :(得分:0)

我想这应该适合你:

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

答案 1 :(得分:0)

所以我在这里做的只是模拟它1000次并在每次模拟上滚动5个骰子并通过递增表示该结果的整数变量(即,1,twos等)来记录每个结果,最后,如果任何整数是> 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;

答案 2 :(得分:0)

情景:

  

骰子五卷某些时间并计算得到至少 3个数字等于(4和5)的估计概率与结果。

流程图:

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

Flowchat diagram

代码:

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%

额外:

您可以在不滚动骰子的情况下计算概率。你需要询问&#34;它们没有发生的概率是多少?&#34;并从1减去(因为补充事件为&#34;没有发生&#34;是&#34;至少有一个发生&#34;)。

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

3的概率等于:enter image description here

3的概率不等于:enter image description here

总之,最终的公式是:enter image description here

实施例

使用上面的公式:

enter image description here

正如你所看到的,每次尝试5次掷骰子,你获得至少三个相同数字的概率就会增加很多。