Simple implementation to demonstrate the Monty Hall Problem (http://en.wikipedia.org/wiki/Monty_Hall_problem)
| package main | |
| import ( | |
| "fmt" | |
| "math/rand" | |
| "os" | |
| "strconv" | |
| "time" | |
| ) | |
| func main() { | |
| count := 0 | |
| rand.Seed(time.Now().Unix()) | |
| max := 100 | |
| if len(os.Args) > 1 { | |
| var err error | |
| max, err = strconv.Atoi(os.Args[1]) | |
| if err != nil { | |
| fmt.Println(err) | |
| max = 100 | |
| } | |
| } | |
| for i := 0; i < max; i++ { | |
| var choices [3]bool | |
| choices[rand.Int31n(3)] = true | |
| pick := rand.Int31n(3) | |
| show := (pick + 1) % 3 | |
| if choices[show] { | |
| show = (show + 1) % 3 | |
| } | |
| swap := (pick + 2) % 3 | |
| if swap == show { | |
| swap = (swap + 1) % 3 | |
| } | |
| if swap == pick { | |
| swap = (swap + 1) % 3 | |
| } | |
| if choices[swap] { | |
| count++ | |
| } | |
| fmt.Printf("%d: chose %d, showed %d, swapped to %d, swap worked: %v\n", i, pick, show, swap, choices[swap]) | |
| } | |
| fmt.Println(count) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment