Verify distribution uniformity/Naive: Difference between revisions
Content deleted Content added
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: |
import std.stdio: writeln; |
||
import std.algorithm: sort; |
|||
/** |
/** |
||
Bin the answers to fn() and check bin counts are within |
Bin the answers to fn() and check bin counts are within |
||
of repeats/bincount. |
+/- delta % of repeats/bincount. |
||
*/ |
*/ |
||
void distCheck(TF)(TF func, int |
void distCheck(TF)(in TF func, in int nRepeats, in double delta) { |
||
int[int] |
int[int] counts; |
||
foreach (i; 0 .. nRepeats) |
|||
counts[func()]++; |
|||
double target = |
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; |
foreach (k, count; counts) |
||
if (abs(target - count) >= deltaCount) |
if (abs(target - count) >= deltaCount) |
||
throw new Exception(format( |
throw new Exception(format( |
||
"distribution potentially skewed for '%s': '%d'\n", |
|||
k, count)); |
|||
foreach (k; counts.keys.sort()) |
|||
writefln(freqs); |
|||
writeln(k, " ", counts[k]); |
|||
writeln(); |
|||
}</lang> |
}</lang> |
||