Monte Carlo methods: Difference between revisions

Content deleted Content added
Line 249: Line 249:
int inside;
int inside;
foreach (i; 0 .. nthrows)
foreach (i; 0 .. nthrows)
if (uniform(0,1.0) ^^ 2 + uniform(0,1.0) ^^ 2 <= 1)
if (hypot(uniform(0,1.0), uniform(0,1.0)) <= 1)
inside++;
inside++;
return 4.0 * inside / nthrows;
return 4.0 * inside / nthrows;
Line 256: Line 256:
void main() {
void main() {
foreach (p; 1 .. 9)
foreach (p; 1 .. 9)
writefln("%11s: %07f", 10 ^^ p, pi(10 ^^ p));
writefln("%9s: %07f", 10^^p, pi(10^^p));
}</lang>
}</lang>