-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (89 loc) · 2.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"fmt"
"math/rand"
)
type door struct {
ID int
Winning bool
Chosen bool
Open bool
}
const (
doorCount = 3
iterationCount = 1000000
)
func main() {
var doors [doorCount]door
var stayedWin int
var stayedLose int
var switchedWin int
var switchedLose int
for range iterationCount {
// Initialize all doors
for i := range doors {
doors[i].ID = i + 1
doors[i].Winning = false
doors[i].Chosen = false
doors[i].Open = false
}
// Pick a door that will have the prize
doors[rand.Intn(doorCount)].Winning = true
// ** Player picks a door
playerFirstChoice := &doors[rand.Intn(len(doors))]
playerFirstChoice.Chosen = true // Mark it as being chosen in the first guess
// ** Host opens one of the remaining doors
// Find out the remaining doors (not chosen)
remainingDoors := make([]*door, 0, doorCount-1)
for i := range doors {
if doors[i].Chosen {
continue
}
remainingDoors = append(remainingDoors, &doors[i])
}
// Of those doors filter out the winning one
doorsThatCanBeOpened := make([]*door, 0)
for i := range remainingDoors {
if remainingDoors[i].Winning {
continue
}
doorsThatCanBeOpened = append(doorsThatCanBeOpened, remainingDoors[i])
}
// Pick one of the doors to open
hostOpenedDoor := doorsThatCanBeOpened[rand.Intn(len(doorsThatCanBeOpened))]
hostOpenedDoor.Open = true
// ** Player chooses a door with or without switching
var playerSecondChoice *door
for i := range doors {
if doors[i].Open || doors[i].Chosen {
continue
}
playerSecondChoice = &doors[i]
}
if playerSecondChoice == nil {
panic("playerSecondChoice is nil")
}
playerSwitched := rand.Intn(2) == 1
if !playerSwitched {
if playerFirstChoice.Winning {
// Player WON by NOT switching the door!
stayedWin++
} else {
// Player LOST by NOT switching the door!
stayedLose++
}
} else {
if playerSecondChoice.Winning {
// Player WON by switching the door!
switchedWin++
} else {
// Player LOST by switching the door!
switchedLose++
}
}
}
fmt.Printf("stayedWin: %v\n", stayedWin)
fmt.Printf("stayedLose: %v\n", stayedLose)
fmt.Printf("switchedWin: %v\n", switchedWin)
fmt.Printf("switchedLose: %v\n", switchedLose)
}