Last active
November 24, 2017 13:55
-
-
Save ymgyt/9970a1e3fc292f5f911b102b8e520a03 to your computer and use it in GitHub Desktop.
Goでモンティ・ホール問題を試してみる ref: https://qiita.com/YmgchiYt/items/1ce7d39309de5406953e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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