Monte Carlo methods: Difference between revisions

Content added Content deleted
m (→‎{{header|Go}}: add variant with x/exp/rand)
Line 904: Line 904:


=={{header|Go}}==
=={{header|Go}}==
'''Using standard library math/rand:'''
<lang go>package main
<lang go>package main


Line 945: Line 946:
3.14149596
3.14149596
</pre>
</pre>
'''Using x/exp/rand:'''

For very careful Monte Carlo studies, you might consider the subrepository rand library. The random number generator there has some advantages such as better known statistical properties and better use of memory.
<lang go>package main

import (
"fmt"
"math"
"time"

"golang.org/x/exp/rand"
)

func getPi(numThrows int) float64 {
inCircle := 0
for i := 0; i < numThrows; i++ {
//a square with a side of length 2 centered at 0 has
//x and y range of -1 to 1
randX := rand.Float64()*2 - 1 //range -1 to 1
randY := rand.Float64()*2 - 1 //range -1 to 1
//distance from (0,0) = sqrt((x-0)^2+(y-0)^2)
dist := math.Hypot(randX, randY)
if dist < 1 { //circle with diameter of 2 has radius of 1
inCircle++
}
}
return 4 * float64(inCircle) / float64(numThrows)
}

func main() {
rand.Seed(uint64(time.Now().UnixNano()))
fmt.Println(getPi(10000))
fmt.Println(getPi(100000))
fmt.Println(getPi(1000000))
fmt.Println(getPi(10000000))
fmt.Println(getPi(100000000))
}</lang>


=={{header|Haskell}}==
=={{header|Haskell}}==