RosettaCodeData/Task/Monte-Carlo-methods/D/monte-carlo-methods-1.d
2023-07-01 13:44:08 -04:00

14 lines
365 B
D

import std.stdio, std.random, std.math;
double pi(in uint nthrows) /*nothrow*/ @safe /*@nogc*/ {
uint inside;
foreach (immutable i; 0 .. nthrows)
if (hypot(uniform01, uniform01) <= 1)
inside++;
return 4.0 * inside / nthrows;
}
void main() {
foreach (immutable p; 1 .. 8)
writefln("%10s: %07f", 10 ^^ p, pi(10 ^^ p));
}