[your code here]

Jeff Pratt me at jpratt.dev
Tue Apr 27 21:04:19 UTC 2021


```
// Estimate π using random integers
void main()
{
     import std.stdio, std.random, std.math, std.conv;
     ulong gcd(ulong a, ulong b)
     {
         if (b == 0) return a;
         return gcd(b, a % b);
     }
     Mt19937_64 gen; gen.seed(unpredictableSeed!ulong);
     int pairCount = 1_000_000;
     int coprimeCount = 0;
     ulong a, b;
     for (int i = 0; i < pairCount; i++) {
         a = gen.front; gen.popFront();
         b = gen.front; gen.popFront();
         if (gcd(a, b) == 1) coprimeCount++;
     }
     double prob = coprimeCount.to!double / pairCount.to!double;
     writefln("π ≈ %.15f", sqrt(6.0 / prob));
}
```


More information about the Digitalmars-d mailing list