# 三门问题（蒙提霍尔悖论）分析与Golang模拟

aside section ._1OhGeD · · 3361 次点击 · · 开始浏览

### 程序模拟

``````package main

import "math/rand"
import "fmt"

func main() {
totalTimes := 1000000
aRightTimes := 0
bRightTimes := 0

for time := 1; time <= totalTimes; time++ {
doors := [3]bool{false, false, false}
doors[rand.Intn(3)] = true

aFirstChoice := rand.Intn(3)
var hostChoice, aChangedChoice int

for i, door := range doors {
if i != aFirstChoice && !door {
hostChoice = i
}
}

for i, _ := range doors {
if i != aFirstChoice && i != hostChoice {
aChangedChoice = i
}
}

var bChoice int

if rand.Intn(2) == 0 {
bChoice = aFirstChoice
} else {
bChoice = aChangedChoice
}

if doors[aChangedChoice] {
aRightTimes++
}

if doors[bChoice] {
bRightTimes++
}
}

fmt.Println("totalTimes: ", totalTimes)
fmt.Println("aChangedChoice: ", aRightTimes)
fmt.Println("bChoice: ", bRightTimes)
}
``````

``````totalTimes:  1000000
aChangedChoice:  667407
bChoice:  499262
``````

### reference

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

### 程序模拟

``````package main

import "math/rand"
import "fmt"

func main() {
totalTimes := 1000000
aRightTimes := 0
bRightTimes := 0

for time := 1; time <= totalTimes; time++ {
doors := [3]bool{false, false, false}
doors[rand.Intn(3)] = true

aFirstChoice := rand.Intn(3)
var hostChoice, aChangedChoice int

for i, door := range doors {
if i != aFirstChoice && !door {
hostChoice = i
}
}

for i, _ := range doors {
if i != aFirstChoice && i != hostChoice {
aChangedChoice = i
}
}

var bChoice int

if rand.Intn(2) == 0 {
bChoice = aFirstChoice
} else {
bChoice = aChangedChoice
}

if doors[aChangedChoice] {
aRightTimes++
}

if doors[bChoice] {
bRightTimes++
}
}

fmt.Println("totalTimes: ", totalTimes)
fmt.Println("aChangedChoice: ", aRightTimes)
fmt.Println("bChoice: ", bRightTimes)
}
``````

``````totalTimes:  1000000
aChangedChoice:  667407
bChoice:  499262
``````