Goでモンティ・ホール問題を試してみる
software design 2017年12月号にて pythonでモンティ・ホール問題の検証をおこなっている記事があったので、Goで試してみました
code
func game(change bool) int {
choice := rand.Intn(3)
prize := rand.Intn(3)
if change {
var opened int
for {
opened = rand.Intn(3)
if opened != prize && opened != choice {
break
}
}
orgChoice := choice
for {
choice = rand.Intn(3)
if choice != opened && choice != orgChoice {
break
}
}
}
if choice == prize {
return 1
}
return 0
}
func play(count int, change bool) float64 {
var point int
for i := 0; i < count; i++ {
point += game(change)
}
return float64(point) / float64(count)
}
func main() {
fmt.Printf("no change: %f\n", play(100000, false))
fmt.Printf("change : %f\n", play(100000, true))
}
結果
no change: 0.333920
change : 0.665360
感想
確かに、選択を変更したほうが、当たる確率が高くなっていました。