Created
February 1, 2015 16:13
-
-
Save shouichi/863ca17762cd1aa13791 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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