Skip to content

Instantly share code, notes, and snippets.

@yaraki
Created April 27, 2014 04:09
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 yaraki/11337359 to your computer and use it in GitHub Desktop.
Save yaraki/11337359 to your computer and use it in GitHub Desktop.
Monty Hall Problem
package main
import (
"fmt"
"math/rand"
)
const doors = 3
const trials = 10000
const seed = 15
func main() {
unchanged_count := 0
changed_count := 0
rand.Seed(seed)
for i := 0; i < trials; i++ {
// Here is the car
answer := rand.Intn(doors)
// First guess by the attendee
first_guess := rand.Intn(doors)
// Hint from the host
hinted := first_guess
for hinted == answer || hinted == first_guess {
hinted = rand.Intn(doors)
}
// Changed guess
second_guess := first_guess
for second_guess == first_guess || second_guess == hinted {
second_guess = rand.Intn(doors)
}
// Check
if first_guess == answer {
unchanged_count++
}
if second_guess == answer {
changed_count++
}
// fmt.Printf("First: %v, Second: %v\n", first_guess == answer, second_guess == answer)
}
fmt.Printf("Changed wins: %v, Unchanged wins: %v\n", changed_count, unchanged_count)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment