Monty Hall problem: Difference between revisions
Content added Content deleted
(Go solution) |
|||
Line 813: | Line 813: | ||
Chance of winning by not switching is 32.82% |
Chance of winning by not switching is 32.82% |
||
Chance of winning by switching is 67.18% |
Chance of winning by switching is 67.18% |
||
=={{header|Go}}== |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"rand" |
|||
) |
|||
func main() { |
|||
games := 1000000 |
|||
var switchWinsCar, keepWinsCar int |
|||
for i := 0; i < games; i++ { |
|||
// simulate game |
|||
carDoor := rand.Intn(3) |
|||
firstChoice := rand.Intn(3) |
|||
var hostOpens int |
|||
if carDoor == firstChoice { |
|||
hostOpens = rand.Intn(2) |
|||
if hostOpens >= carDoor { |
|||
hostOpens++ |
|||
} |
|||
} else { |
|||
hostOpens = 3 - carDoor - firstChoice |
|||
} |
|||
remainingDoor := 3 - hostOpens - firstChoice |
|||
// some assertions that above code produced a valid game state |
|||
if carDoor < 0 || carDoor > 2 { |
|||
panic("car behind invalid door") |
|||
} |
|||
if firstChoice < 0 || firstChoice > 2 { |
|||
panic("contestant chose invalid door") |
|||
} |
|||
if hostOpens < 0 || hostOpens > 2 { |
|||
panic("host opened invalid door") |
|||
} |
|||
if hostOpens == carDoor { |
|||
panic("host opened door with car") |
|||
} |
|||
if hostOpens == firstChoice { |
|||
panic("host opened contestant's first choice") |
|||
} |
|||
if remainingDoor < 0 || remainingDoor > 2 { |
|||
panic("remaining door invalid") |
|||
} |
|||
if remainingDoor == firstChoice { |
|||
panic("remaining door same as contestant's first choice") |
|||
} |
|||
if remainingDoor == hostOpens { |
|||
panic("remaining door same as one host opened") |
|||
} |
|||
// tally results |
|||
if firstChoice == carDoor { |
|||
keepWinsCar++ |
|||
} |
|||
if remainingDoor == carDoor { |
|||
switchWinsCar++ |
|||
} |
|||
} |
|||
fmt.Println("In", games, "games,") |
|||
fmt.Println("switching doors won the car", switchWinsCar, "times,") |
|||
fmt.Println("keeping same door won the car", keepWinsCar, "times.") |
|||
}</lang> |
|||
Output: |
|||
<pre> |
|||
In 1000000 games, |
|||
switching doors won the car 666358 times, |
|||
keeping same door won the car 333642 times. |
|||
</pre> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |