Skip to content

Instantly share code, notes, and snippets.

@ymgyt
Last active November 24, 2017 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ymgyt/9970a1e3fc292f5f911b102b8e520a03 to your computer and use it in GitHub Desktop.
Save ymgyt/9970a1e3fc292f5f911b102b8e520a03 to your computer and use it in GitHub Desktop.
Goでモンティ・ホール問題を試してみる ref: https://qiita.com/YmgchiYt/items/1ce7d39309de5406953e
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment