Java概率统计输出
Java output in probability counting
考虑到数学输出与程序给出的结果之间的差异,我遇到了一个问题。
我想以1/6的概率统计两次获得相同数字的概率,应该是1 in 1/6 * 1/6 = 36
。但是,我得到的答案在 42-43 分之 1 之间。怎么了?
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);
}
考虑到我这样做 1000000
次(countGen
)并取结果的平均值。提前致谢。
运行以下代码:
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
Or about 1 in 35.8512888538343
那么,为什么 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));
你算错了。您正在将抛出次数 (countTot) 与成功 双重比较 的次数进行比较。你应该得到它的 1/72。但是你没有得到它,因为如果第一对不匹配就提前退出。
下面的代码给出了正确答案。这不是很好,我会重命名大部分东西,但我想尽可能保持它与原来的相似
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);
}
您正在计算不允许重叠的连续匹配对的数量。如果你得到一个由 3 个随机数组成的序列,它们都相等,你应该数 2 对,但你只数了一个。不允许重叠意味着一对取决于它之前发生的事情。为了能够乘以概率,您必须保证事件是独立的。
考虑到数学输出与程序给出的结果之间的差异,我遇到了一个问题。
我想以1/6的概率统计两次获得相同数字的概率,应该是1 in 1/6 * 1/6 = 36
。但是,我得到的答案在 42-43 分之 1 之间。怎么了?
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);
}
考虑到我这样做 1000000
次(countGen
)并取结果的平均值。提前致谢。
运行以下代码:
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
Or about 1 in 35.8512888538343
那么,为什么 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));
你算错了。您正在将抛出次数 (countTot) 与成功 双重比较 的次数进行比较。你应该得到它的 1/72。但是你没有得到它,因为如果第一对不匹配就提前退出。
下面的代码给出了正确答案。这不是很好,我会重命名大部分东西,但我想尽可能保持它与原来的相似
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);
}
您正在计算不允许重叠的连续匹配对的数量。如果你得到一个由 3 个随机数组成的序列,它们都相等,你应该数 2 对,但你只数了一个。不允许重叠意味着一对取决于它之前发生的事情。为了能够乘以概率,您必须保证事件是独立的。