Unbias a random generator: Difference between revisions

Updated D code
(use proper range)
(Updated D code)
Line 292:
<lang d>import std.stdio, std.random;
 
bool biased(in int n) /*nothrow*/ {
return uniform(0.0, 1.0) < (1.0 / n);
}
 
bool unbiased(in int n) /*nothrow*/ {
boolwhile a,(true) b;{
immutable bool a = biased(n);
do {
if (a != biased(n);)
b = biased(n) return a;
} while (a == b);
return a;
}
 
void main() {
enum int M = 50_00020_000_000;
foreach (n; 3 .. 7) {
int c1a1, c2a2; // accumulators
foreach (i; 0 .. M) {
c1a1 += biased(n);
c2a2 += unbiased(n);
}
writefln("%d: %2.2f3f%% %2.2f3f%%", n, 100.0*c1/M, 100.0*c2/M);
100.0 * a1 / M, 100.0 * a2 / M);
}
}</lang>
Output:
<pre>3: 33.12352% 50.15002%
4: 25.13006% 5049.02989%
5: 19.61995% 5049.01989%
6: 16.70679% 4950.98007%</pre>
 
=={{header|Euphoria}}==
Anonymous user