Verify distribution uniformity/Naive: Difference between revisions

Content deleted Content added
Sonia (talk | contribs)
m →‎{{header|Go}}: library changes
Updated D code
Line 213: Line 213:
<lang d>import std.math: abs;
<lang d>import std.math: abs;
import std.string: format;
import std.string: format;
import std.stdio: writefln;
import std.stdio: writeln;
import std.algorithm: sort;


/**
/**
Bin the answers to fn() and check bin counts are within +/- delta %
Bin the answers to fn() and check bin counts are within
of repeats/bincount.
+/- delta % of repeats/bincount.
*/
*/
void distCheck(TF)(TF func, int nrepeats, double delta) {
void distCheck(TF)(in TF func, in int nRepeats, in double delta) {
int[int] freqs;
int[int] counts;
for (int i; i < nrepeats; i++)
foreach (i; 0 .. nRepeats)
freqs[func()]++;
counts[func()]++;
double target = nrepeats / cast(double)freqs.length;
immutable double target = nRepeats / cast(double)counts.length;
int deltaCount = cast(int)(delta / 100.0 * target);
immutable int deltaCount = cast(int)(delta / 100.0 * target);


foreach (k, count; freqs)
foreach (k, count; counts)
if (abs(target - count) >= deltaCount)
if (abs(target - count) >= deltaCount)
throw new Exception(format("distribution potentially skewed for '%s': '%d'\n",
throw new Exception(format(
k, count));
"distribution potentially skewed for '%s': '%d'\n",
k, count));


foreach (k; counts.keys.sort())
writefln(freqs);
writeln(k, " ", counts[k]);
writeln();
}</lang>
}</lang>