Monte Carlo methods: Difference between revisions
Content added Content deleted
(Added an example in Erlang) |
|||
Line 390: | Line 390: | ||
100000: 3.14104 |
100000: 3.14104 |
||
1000000: 3.141064 |
1000000: 3.141064 |
||
</pre> |
|||
<lang lisp>(def pi-seq |
|||
(pmap first |
|||
(iterate |
|||
(fn [[pi within total]] |
|||
[ (double (* (/ within total) 4)) |
|||
(if (<= (+ (Math/pow (rand) 2) (Math/pow (rand) 2)) 1) (inc within) within) |
|||
(inc total) ] ) |
|||
[0.0 0N 1N]))) |
|||
(nth pi-seq 1000000) |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
3.140492 |
|||
</pre> |
</pre> |
||