42 lines
1.3 KiB
Text
42 lines
1.3 KiB
Text
type Prize
|
|
enum do int GOAT, CAR end
|
|
type Door
|
|
model
|
|
int id
|
|
Prize prize
|
|
new by int ←id, Prize ←prize do end
|
|
fun asText ← text by block do return "(id:" + me.id + ", prize:" + me.prize.value + ")" end
|
|
end
|
|
type Player
|
|
model
|
|
Door choice
|
|
fun choose ← void by List doors
|
|
me.choice ← doors[random(3)]
|
|
end
|
|
end
|
|
type Monty
|
|
model
|
|
fun setPrize ← void by List doors, Prize prize
|
|
doors[random(3)].prize ← prize
|
|
end
|
|
end
|
|
type MontyHallProblem
|
|
int ITERATIONS ← 1000000
|
|
Map counter ← text%int[ "keep" ⇒ 0, "switch" ⇒ 0 ]
|
|
writeLine("Simulating " + ITERATIONS + " games:")
|
|
for int i ← 0; i < ITERATIONS; i++
|
|
if i % 100000 æ 0 do write(".") end
|
|
^|three numbered doors with no cars for now|^
|
|
List doors ← Door[Door(1, Prize.GOAT), Door(2, Prize.GOAT), Door(3, Prize.GOAT)]
|
|
Monty monty ← Monty() # set up Monty
|
|
monty.setPrize(doors, Prize.CAR) # Monty randomly sets the car behind one door
|
|
Player player ← Player() # set up the player
|
|
player.choose(doors) # the player makes a choice
|
|
^|here Monty opens a door with a goat;
|
|
|behind the ones that are still closed there is a car and a goat,
|
|
|so that the player *always* wins by keeping or switching.
|
|
|^
|
|
counter[when(player.choice.prize æ Prize.CAR, "keep", "switch")]++
|
|
end
|
|
writeLine()
|
|
writeLine(counter)
|