JAVA-解决Bertrand';s盒悖论
我试图创建一个代码块来模拟Bertrand的box悖论的100000次迭代。以下是问题(摘自维基百科): 有三个盒子:JAVA-解决Bertrand';s盒悖论,java,statistics,Java,Statistics,我试图创建一个代码块来模拟Bertrand的box悖论的100000次迭代。以下是问题(摘自维基百科): 有三个盒子: a box containing two gold coins, a box containing two silver coins, a box containing one gold coin and one silver coin. “悖论”是指在随机选择一个盒子并随机取出一枚硬币(如果碰巧是金币)后,从同一盒子中取出的下一枚硬币也可能是金币 根据贝叶斯定理,答案应该是
a box containing two gold coins,
a box containing two silver coins,
a box containing one gold coin and one silver coin.
“悖论”是指在随机选择一个盒子并随机取出一枚硬币(如果碰巧是金币)后,从同一盒子中取出的下一枚硬币也可能是金币
根据贝叶斯定理,答案应该是66%。
但我的代码是返回58%的第二个金币的机会在选定的框
代码如下:
double probabilityGold = 0;
double probabilitySilver = 0;
Random random = new Random();
String[][] boxesOfCoins = { { "S", "G" }, { "S", "S" }, { "G", "G" } };
int matches = 100000;
while( matches > 0 ) {
int drawBox = random.nextInt( 3 );
int drawCoin = random.nextInt( 2 );
String coinDrawn = boxesOfCoins[ drawBox ][ drawCoin ];
// to ensure that the first coin picked is a gold coin
while( coinDrawn.equals( "S" ) ) {
drawBox = random.nextInt( 3 );
drawCoin = random.nextInt( 2 );
coinDrawn = boxesOfCoins[ random.nextInt( 3 ) ][ random.nextInt( 2 ) ];
}
if( drawCoin == 1 ) {
String secondCoin = boxesOfCoins[ drawBox ][ 0 ];
if( secondCoin.equals( "G" ) ) {
probabilityGold++;
} else {
probabilitySilver++;
}
} else {
String secondCoin = boxesOfCoins[ drawBox ][ 1 ];
if( secondCoin.equals( "G" ) ) {
probabilityGold++;
} else {
probabilitySilver++;
}
}
matches--;
}
System.out.println( probabilityGold/100000 );
System.out.println( probabilitySilver/100000 );
我的错误可能是执行了一个寻找硬币的游戏,但唉,我无法解决它。
如何获得所需的66%输出?注意,在内部while循环中,您没有使用drawBox drawCoin的计算值来拾取硬币?是的,陀螺仪无齿轮,这是我的问题。现在它正在按预期工作。非常感谢你。