Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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