// http://ja.wikipedia.org/wiki/モンティ・ホール問題
#include <iostream>
#include <cstdlib>
using namespace std;
int
main(int, char**)
{
const int N = 100000;
const int n = 3; // # of doors
int p1 = 0;
int p2 = 0;
for (int k = 0; k < N; k++) {
bool x[n] = {};
int hit = rand() % n;
x[hit] = true;
// player's 1st choice
int a1 = rand() % n;
// monty's suggestion
int hint;
for (;;) {
hint = rand() % n;
if (hint != a1 && hint != hit)
break;
}
// player's 2nd choice
int a2;
for (;;) {
a2 = rand() % n;
if (a2 != a1 && a2 != hint)
break;
}
if (a1 == hit)
p1++;
if (a2 == hit)
p2++;
}
cout << "p1 " << (1.0 * p1 / N) << endl;
cout << "p2 " << (1.0 * p2 / N) << endl;
return 0;
}
% c++ monty.cc
% ./a.out
p1 0.33477
p2 0.66523
%
ドアの数が100の場合は?