Skip to content

Instantly share code, notes, and snippets.

@paulhankin
Created February 27, 2020 09:11
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 paulhankin/cc766c4e50f73a91c89ef7fbd74ee9db to your computer and use it in GitHub Desktop.
Save paulhankin/cc766c4e50f73a91c89ef7fbd74ee9db to your computer and use it in GitHub Desktop.
monte carlo pi
package main
import (
"fmt"
"testing"
"github.com/db47h/rand64/xoroshiro"
)
const N = 10_000_000
func BenchmarkPi(b *testing.B) {
for n := 0; n < b.N; n++ {
var S int64
var rng xoroshiro.Rng128P
rng.Seed(1111)
for i := 0; i < N; i++ {
x := float64(rng.Uint64()) / 18446744073709551615
y := float64(rng.Uint64()) / 18446744073709551615
if x*x+y*y <= 1 {
S += 1
}
}
if n == 0 {
fmt.Println(4 * float64(S) / N)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment