Skip to content

Instantly share code, notes, and snippets.

@shouichi
Created February 1, 2015 16:13
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 shouichi/863ca17762cd1aa13791 to your computer and use it in GitHub Desktop.
Save shouichi/863ca17762cd1aa13791 to your computer and use it in GitHub Desktop.
// Calculates Pi using Monte Carlo. See http://montepie.herokuapp.com/
package main
import (
"fmt"
"math"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().Unix())
n := 10000
in := 0
for i := 0; i < n; i++ {
x := rand.Float64()
y := rand.Float64()
d := math.Sqrt(x*x + y*y)
if d <= 1 {
in++
}
}
pi := 4.0 * float64(in) / float64(n)
fmt.Println(pi)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment