Jump to content

Verify distribution uniformity/Naive: Difference between revisions

Updated D code
m (→‎{{header|Go}}: library changes)
(Updated D code)
Line 213:
<lang d>import std.math: abs;
import std.string: format;
import std.stdio: writeflnwriteln;
import std.algorithm: sort;
 
/**
Bin the answers to fn() and check bin counts are within +/- delta %
+/- delta % of repeats/bincount.
*/
void distCheck(TF)(in TF func, in int nrepeatsnRepeats, in double delta) {
int[int] freqscounts;
forforeach (int i; i0 <.. nrepeats; i++nRepeats)
freqscounts[func()]++;
immutable double target = nrepeatsnRepeats / cast(double)freqscounts.length;
immutable int deltaCount = cast(int)(delta / 100.0 * target);
 
foreach (k, count; freqscounts)
if (abs(target - count) >= deltaCount)
throw new Exception(format("distribution potentially skewed for '%s': '%d'\n",
"distribution potentially skewed for '%s': k'%d'\n", count));
k, count));
 
foreach (k; counts.keys.sort())
writefln(freqs);
writeln(k, " ", counts[k]);
writeln();
}</lang>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.