We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Ok so I am working on a simulation of choosing to switch when confronted by the Monty Hall Problem. The problem is that it always comes out as 33% when it should be about 50%. What am I doing wrong?
boolean[] door = new boolean[3];
int choice, next, switchthat, y, yes, no;
void setup() {
size(500, 500);
yes = 0;
y = 0;
}
void draw() {
background(0);
pickDoor();
rect(0, height-(yes*height/(yes+no)), width/2, (yes*height/(yes+no)));
rect(width/2, height-(no*height/(yes+no)), width/2, no*height/(yes+no));
text(yes*100/(yes+no), 100, 100);
}
void pickDoor() {
door[0] = false;
door[1] = false;
door[2] = false;
y = int(random(0, 3));
door[y] = true;
pickOne();
}
void pickOne() {
choice = int(random(0, 3));
pickNext();
}
void pickNext() {
next = int(random(0, 3));
if (next == choice) {
pickNext();
} else {
switchit();
}
}
void switchit() {
switchthat = int(random(0, 3));
if (switchthat == next) {
switchit();
} else if (switchthat == choice) {
switchit();
} else {
check();
}
}
void check() {
if (door[switchthat]==true) {
yes++;
} else {
no++;
}
}
Answers
If you commented the code it would help us understand the semantics because it does not look like the Monty Hall to me. Also the use of recursion makes it harder, it could easily be written without recursion
The pickNext doesn't look right. At this point Monty opens a door that is 'false' and shows it to the competitor. The competitor has the choice of keeping his choice or selecting the third door when his chances increase to 50%
I suggest that you replace the
pickOne
andpickNext
methods with these. They do the same job (still 33%) but get rid of the recursion making the logic clearer.Your premise is wrong. If the contestant switches there is a 67% chance of winning the prize behind the correct door - see
Monty Hall Problem
I also made a mistake is assuming you checked you premise. :)