Created
February 27, 2020 09:11
-
-
Save paulhankin/cc766c4e50f73a91c89ef7fbd74ee9db to your computer and use it in GitHub Desktop.
monte carlo pi
This file contains 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
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