Skip to content

Instantly share code, notes, and snippets.

@jonbodner
Created July 5, 2014 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonbodner/4b99bc3ccc024d74babf to your computer and use it in GitHub Desktop.
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)
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