Skip to content

Instantly share code, notes, and snippets.

@skhatri
Created August 7, 2022 11:24
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 skhatri/90b666c31dad9e3a52811132507dd3b4 to your computer and use it in GitHub Desktop.
Save skhatri/90b666c31dad9e3a52811132507dd3b4 to your computer and use it in GitHub Desktop.
value of pi
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
var sum int64 = 0
var i int64 = 0
var tot int64 = 10 * 1000 * 1_000_000
start := time.Now()
for {
x := rand.Float64() * 2 - 1
y := rand.Float64() * 2 - 1
d := x*x + y*y
if d <= 1 {
sum += 1
}
i += 1
if i >= tot {
break
}
}
var pi float64 = (4.0 * float64(sum)) / (float64(tot - 1) * 1.0)
dur := time.Since(start)
fmt.Printf("value of pi %f calculated in %d ms with %d iterations", pi, dur.Milliseconds(), tot)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment