问题描述
我正在尝试创建一个代码块来模拟 Bertrand 盒悖论的 100000 次迭代。这是问题(取自维基百科):
一共有三个盒子:
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% 输出?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)