Verify distribution uniformity/Naive: Difference between revisions

Content added Content deleted
m (→‎{{header|JavaScript}}: not recommended to use for..in on arrays.)
Line 166: Line 166:
NIL
NIL
#<EQL Hash Table{7} 200CB5BB></pre>
#<EQL Hash Table{7} 200CB5BB></pre>

=={{header|D}}==

<lang d>import std.math: abs;
import std.string: format;
import std.stdio: writefln;

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

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

writefln(freqs);
}</lang>


=={{header|Haskell}}==
=={{header|Haskell}}==