掷 5 个骰子,看 3 个是否相同

Roll 5 dice and see if 3 are the same

全题:模拟反复试验,估计掷出5个骰子,至少得到3个相同数字的概率。

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

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;

    }

我想这应该适合你:

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

所以我在这里做的只是模拟它 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;

场景:

Dice five rolls certain times and calculate the estimated probability of getting at least 3 numbers equals (4 and five inclusive) with the results.

流程图:

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

代码:

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]

No number is repeated 3 times

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

Yay! one number is repeated at least 3 times

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

Yay! one number is repeated at least 3 times

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

At least three equals count: 119

Less than three equals count: 448

Trials: 567

Estimated probability: 20.98765432098765%


额外:

不用掷骰子也能计算概率。您需要询问 "What is the probability that none of them happen?" 并从 1 中减去(因为 "none happen" 的互补事件是 "at least one happens")。

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

3等于的概率:

3不等于的概率:

综上所述,最终公式为:

示例:

用上面的公式:

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