Monte Carlo methods: Difference between revisions

m
Line 392:
</pre>
 
<lang lisp>(defdefn pi-seqexperiment
(pmap first[]
(if (<= (+ (Math/pow (rand) 2) (Math/pow (rand) 2)) 1) (inc1 within0) within)
(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])))
 
(nthdefn pi-seqestimate 1000000)
[n]
(* 4 (float (/ (reduce + (take n (repeatedly experiment))) n))))
 
(pi-estimate 10000)
</lang>
 
{{out}}
<pre>
3.1404921347999572753906
</pre>
 
Anonymous user