Verify distribution uniformity/Naive: Difference between revisions
Content added Content deleted
(Add Forth) |
(→{{header|Java}}: added Java) |
||
Line 869: | Line 869: | ||
|Distribution is potentially skewed: assert |
|Distribution is potentially skewed: assert |
||
| errmsg assert(delta*expected)>|expected-{:"1 freqtable</lang> |
| errmsg assert(delta*expected)>|expected-{:"1 freqtable</lang> |
||
=={{header|Java}}== |
|||
{{trans|D}} |
|||
{{works with|Java|8}} |
|||
<lang java>import static java.lang.Math.abs; |
|||
import java.util.*; |
|||
import java.util.function.IntSupplier; |
|||
public class Test { |
|||
static void distCheck(IntSupplier f, int nRepeats, double delta) { |
|||
Map<Integer, Integer> counts = new HashMap<>(); |
|||
for (int i = 0; i < nRepeats; i++) |
|||
counts.compute(f.getAsInt(), (k, v) -> v == null ? 1 : v + 1); |
|||
double target = nRepeats / (double) counts.size(); |
|||
int deltaCount = (int) (delta / 100.0 * target); |
|||
counts.forEach((k, v) -> { |
|||
if (abs(target - v) >= deltaCount) |
|||
System.out.printf("distribution potentially skewed " |
|||
+ "for '%s': '%d'%n", k, v); |
|||
}); |
|||
counts.keySet().stream().sorted().forEach(k |
|||
-> System.out.printf("%d %d%n", k, counts.get(k))); |
|||
} |
|||
public static void main(String[] a) { |
|||
distCheck(() -> (int) (Math.random() * 5) + 1, 1_000_000, 1); |
|||
} |
|||
}</lang> |
|||
<pre>1 200439 |
|||
2 201016 |
|||
3 199406 |
|||
4 199869 |
|||
5 199270</pre> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |