RosettaCodeData/Task/Monty-Hall-problem/Go/monty-hall-problem.go

28 lines
723 B
Go
Raw Permalink Normal View History

2013-04-10 21:29:02 -07:00
package main
import (
2013-10-27 22:24:23 +00:00
"fmt"
"math/rand"
"time"
2013-04-10 21:29:02 -07:00
)
func main() {
2013-10-27 22:24:23 +00:00
games := 100000
r := rand.New(rand.NewSource(time.Now().UnixNano()))
2013-04-10 21:29:02 -07:00
2013-10-27 22:24:23 +00:00
var switcherWins, keeperWins, shown int
for i := 0; i < games; i++ {
doors := []int{0, 0, 0}
doors[r.Intn(3)] = 1 // Set which one has the car
choice := r.Intn(3) // Choose a door
for shown = r.Intn(3); shown == choice || doors[shown] == 1; shown = r.Intn(3) {}
switcherWins += doors[3 - choice - shown]
keeperWins += doors[choice]
}
floatGames := float32(games)
fmt.Printf("Switcher Wins: %d (%3.2f%%)\n",
switcherWins, (float32(switcherWins) / floatGames * 100))
fmt.Printf("Keeper Wins: %d (%3.2f%%)",
keeperWins, (float32(keeperWins) / floatGames * 100))
2013-04-10 21:29:02 -07:00
}