//////////////////////////////////////////////////////////////////////////////// // The Monty Hall problem (vos Savant 1990) //////////////////////////////////////////////////////////////////////////////// // // Suppose you're on a game show, and you're given the choice of three doors: // Behind one door is a car; behind the others, goats. You pick a door, say #1, // and the host, who knows what's behind the doors, opens another door, say #3, // which has a goat. He then says to you, "Do you want to pick door #2?" // Is it to your advantage to switch your choice? // //////////////////////////////////////////////////////////////////////////////// def montysStrategy(gtL, gtR) { return if flip(1/2) { gtL } else { gtR }; } def main() { // positions: car, left goat, right goat car := uniformInt(0,2); gtL := if car == 0 { 1 } else { 0 }; gtR := if car == 2 { 1 } else { 2 }; // doors: 0 means goat, 1 means car x := [0,0,0]; x[car] = 1; // choices: player, Monty, switch p := uniformInt(0,2); m := if p == gtL { gtR } else if p == gtR { gtL } else { montysStrategy(gtL, gtR) }; s := if p != car { car } else if m == gtL { gtR } else { gtL }; // observations observe(p == 0 && m == 2); return x[s]; }