概率计数中的Java输出

时间:2016-07-28 20:01:02

标签: java probability

考虑到数学输出与程序给出的差异,我遇到了一个问题。

我想计算以1/6概率两次获得相同数字的概率,其应为1 in 1/6 * 1/6 = 36。但是,我得到了42-43之间的答案。有什么问题?

int guess = (int) (Math.random() * 6);
int real = (int) (Math.random() * 6);
int countTot = 0;
int countReal = 0;
int countGen = 0;

while (true) {
    if (countReal == 2) {
        countGen++;
        countReal = 0;
        if (countGen == 1000000) {
            System.out.println("Probability: 1 in " + countTot/countGen);
            System.exit(0);
        }
    }
    if (guess == real) {
        countReal++;
        countTot++;
    } else {
        countReal = 0;
        countTot++;
    }
    guess = (int) (Math.random() * 6);
    real = (int) (Math.random() * 6);
}

考虑我执行1000000countGen次并取结果的平均值。提前谢谢。

3 个答案:

答案 0 :(得分:3)

运行以下代码:

int n = 1_000_000;
int count = 0;
Random rnd = new Random();

for (int i = 0; i < n; i++) {
    int a = rnd.nextInt(6);
    int b = rnd.nextInt(6);
    int c = rnd.nextInt(6);
    int d = rnd.nextInt(6);

    if (a == b && c == d) {
        count++;
    }
}

System.out.println(count + " / " + n);
System.out.println("Or about 1 in " + (n * 1.0 / count));

给出

  

27893/1000000
  或者在35.8512888538343中约有1个

那么,为什么你会在42中获得1分?

考虑一下,如果你得到2个相同的数字,你可以增加countReal。如果第二次得到2个相同的数字,则再次递增countReal(然后将其重置为零)。如果您获得2个相同的 ,则您已经中止计算您的运行。这个可能会影响你的概率。

另一种说法:

int n = 1_000_000;
int count = 0;
Random rnd = new Random();

boolean matched_last_time = false;
for (int i = 0; i < n; i++) {
    int a = rnd.nextInt(6);
    int b = rnd.nextInt(6);
    boolean match = a == b;

    if (match && matched_last_time) {
        count++;
        // match = false;  // Uncomment this line, & probability changes to 1 in 42
    }
    matched_last_time = match;
}

System.out.println(count + " / " + n);
System.out.println("Or about 1 in " + (n * 1.0 / count));

答案 1 :(得分:2)

您在不允许重叠的情况下计算连续匹配对的数量。如果你得到一个3个随机数的序列,它们都是相等的,你应该计算2对,但你只计算一个。不允许重叠意味着一对取决于它之前的内容。为了能够增加概率,您必须保证事件是独立的。

答案 2 :(得分:1)

You are counting it wrong. You are comparing number of throws (countTot) to number of successful double comparisons. What you should get it 1/72. But you are not getting it, because if early exit if first pair is not matching.

Code below gives correct answer. It is not really nice, I would rename most of things, but I wanted to keep it as similar to original as possible

int guess = (int) (Math.random() * 6);
    int real = (int) (Math.random() * 6);
    int countTot = 0;
    int countReal = 0;
    int countGen = 0;

    while (true) {
        if (countReal == 2) {
            countGen++;
            countReal = 0;
            if (countGen == 1000000) {
                System.out.println("Probability: 1 in " + (countTot/2)/countGen);
                System.exit(0);
            }
        }
        if (guess == real) {
            countReal++;
            countTot++;
        } else {
            countTot++;
            if ( countReal == 0 ) {
                countTot++;
            }
            countReal = 0;
        }
        guess = (int) (Math.random() * 6);
        real = (int) (Math.random() * 6);
    }