LoginSignup
6
6

More than 5 years have passed since last update.

Goでモンティ・ホール問題を試してみる

Last updated at Posted at 2017-11-21

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

感想

確かに、選択を変更したほうが、当たる確率が高くなっていました。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6