Unbias a random generator: Difference between revisions
Content added Content deleted
(Updated D code) |
(Updated D code) |
||
Line 290: | Line 290: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio, std.random; |
<lang d>import std.stdio, std.random, std.algorithm, std.range; |
||
bool biased(in int n) /*nothrow*/ { |
bool biased(in int n) /*nothrow*/ { |
||
Line 305: | Line 305: | ||
void main() { |
void main() { |
||
alias reduce!q{a + b} sum; /**/ |
|||
enum int M = 20_000_000; |
enum int M = 20_000_000; |
||
foreach (n; 3 .. 7) { |
foreach (n; 3 .. 7) { |
||
immutable a1 = sum(0, map!(_=> biased(n))(iota(M))); |
|||
immutable a2 = sum(0, map!(_=> unbiased(n))(iota(M))); |
|||
a1 += biased(n); |
|||
a2 += unbiased(n); |
|||
} |
|||
writefln("%d: %2.3f%% %2.3f%%", n, |
writefln("%d: %2.3f%% %2.3f%%", n, |
||
100.0 * a1 / M, 100.0 * a2 / M); |
100.0 * a1 / M, 100.0 * a2 / M); |