4 ms·
See for yourself: import java.util.List; import java.util.Random; import java.util.stream.Collectors; import java.util.stream.Stream; public class
by rho4 5y ago
See for yourself:
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class MontyHall {
public static void main(String[] args) {
Random random = new Random();
final boolean GUEST_ALWAYS_SWITCHES = true /* change guest strategy */;
final boolean HOST_IS_DRUNK = false /* simulate host knowledge */;
final boolean GUEST_WINS_IF_HOST_PICKS_PRIZE_BY_ACCIDENT = true;
final double GAME_COUNT = 1_000_000;
double winCount = 0;
for(int i = 0; i < GAME_COUNT; i++) {
List<String> remainingDoors = Stream.of("A", "B", "C").collect(Collectors.toList());
String prizeDoor = remainingDoors.get(random.nextInt(3));
String guestInitialPick = remainingDoors.remove(random.nextInt(remainingDoors.size()));
String hostPick;
if(HOST_IS_DRUNK) {
hostPick = /* host picks random */ remainingDoors.remove(random.nextInt(remainingDoors.size()));
}
else {
// host is sober and always opens empty door
hostPick = remainingDoors.get(0).equals(prizeDoor) ? remainingDoors.remove(1) : remainingDoors.remove(0);
}
if(hostPick.equals(prizeDoor)) {
if(GUEST_WINS_IF_HOST_PICKS_PRIZE_BY_ACCIDENT) {
winCount++;
}
continue /* insta win/loss */;
}
// guest decides to stick with inital pick or switch
String guestFinalPick = guestInitialPick;
if(GUEST_ALWAYS_SWITCHES) {
guestFinalPick = remainingDoors.get(0);
}
if(guestFinalPick.equals(prizeDoor)) {
winCount++;
}
}
System.out.println(String.format("Win rate: %.2f", winCount / GAME_COUNT));
}
}
- lmm 5y agoTry setting GUEST_ALWAYS_SWITCHES=false, you'll find your winrate is still 2/3. In the if(hostPick.equals(prizeDoor)) block, rather than maybe incrementing winCount, you need to decrement GAME_COUNT (or rather the number that you're going to divide by at the end - don't reduce the number of iterations) - we're talking about the probability when you're deciding whether to switch (so after the host has already opened the door and not hit the prize).
- rho4 5y agoMy statement was that if you always switch, it doesn't matter whether the host is blind or not. But I must say it is an interesting find of the simulation that your win rate is always 2/3 independent of your strategy (provided the host is blind and you win when the host makes a mistake). I'm always talking about the probability of winning the complete game when sticking to a certain strategy. I never look at intermediate probabilities. Decrementing the game count or the divisor would be the like crossing out or ignoring decision tree branches. If you change the game rules and say the game is repeated if the drunken host picks the prize, then the simulation would have to repeat until the guest has either won or lost, but the game count would still remain the same (= the number of times a simulated guest is invited to the game show).
- lmm 5y ago> My statement was that if you always switch, it doesn't matter whether the host is blind or not. Well, it does matter unless you have the rather unusual rule that you win if the host reveals the prize - normally it would make a lot more sense to say you lose in that case. (In fact this is the deep reason why switching works in the original problem - if you switch, then you win if a blind host "would have" revealed the prize, whereas if you don't switch, you lose). The relevant general fact is that if the host is blind, your strategy never makes any difference. > Decrementing the game count or the divisor would be the like crossing out or ignoring decision tree branches. Well, there is no decision to be made if the host revealed the prize - you never get the choice of whether to switch or not. When we ask about the probabilities after a certain choice, at the point where you're making that choice you already know that you've certainly had to make that choice. Otherwise could equally well say that you have to include the probability of getting through to the final round, the probability of getting onto the gameshow in the first place, ...
- rho4 5y agoYou said: "I can make sense of it by drawing out the probability trees and see that it's 1/2 rather than 2/3 in this case" (Edit: sorry that was not your statement, but that from furyofantares.) Now you agreed that it is still 2/3, so I still stand by my statement "As long as the host doesn't accidentally open the prize door, it doesn't matter whether he forgot or not." > Well, it does matter unless you have the rather unusual rule that you win if the host reveals the prize If you change the game and say the host is blind, you have to also make a rule about what happens if the blind host reveals the prize. Otherwise how could you decide on a strategy as a guest? > Well, there is no decision to be made if the host revealed the prize No chance for the guest to switch, yes, but it is still a branch in the whole probability tree, and you have to count it either as a win or a loss for the guest. They can't just all go home and pretend the show didn't happen. > Otherwise could equally well say that you have to include the probability of getting through to the final round, the probability of getting onto the gameshow in the first place, ... The problem statement is: "What strategy (stick or switch) is best for the guest (= has higher probability to win), provided he gets into the game show final." No ambiguity here.