Unbias a random generator: Difference between revisions

Updated D entry
(→‎{{header|Ruby}}: Too much ones, not enough zeros...changed rand_n)
(Updated D entry)
Line 372:
 
=={{header|D}}==
<lang d>import std.stdio, std.random, std.algorithm, std.range, std.functional;
 
alias sum = alias curry!(reduce!q{a + b}, sum0); /**/
 
bool biased(in int n) /*nothrow*/ {
Line 385 ⟶ 387:
 
void main() {
alias reduce!q{a + b} sum; /**/
enum int M = 500_000;
foreach (immutable n; 3 .. 7) {
immutable a1 = sum(0, iota(M).map!(_=> biased(n))()),
a2 = sum(0, iota(M).map!(_=> unbiased(n))());
writefln("%d: %2.3f%% %2.3f%%", n,
100M.iota.0map!(_=> n.biased).sum * a1 / M, 100.0 * a2 / M);,
a2 = sum(0, iota(M).iota.map!(_=> n.unbiased(n))().sum * 100.0 / M);
}
}</lang>
{{out}}