Created
July 5, 2014 22:54
-
-
Save jonbodner/4b99bc3ccc024d74babf to your computer and use it in GitHub Desktop.
Simple implementation to demonstrate the Monty Hall Problem (http://en.wikipedia.org/wiki/Monty_Hall_problem)
This file contains 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
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