Verify distribution uniformity/Naive: Difference between revisions

m
→‎{{header|JavaScript}}: not recommended to use for..in on arrays.
(add JavaScript)
m (→‎{{header|JavaScript}}: not recommended to use for..in on arrays.)
Line 235:
<lang javascript>function distcheck(random_func, times, opts) {
if (opts === undefined) opts = {}
opts['delta'] = opts['delta'] || 1.52;
 
var count = {}, vals = [];
Line 252:
var tolerance = target * opts['delta'] / 100;
 
for (var vali in= 0; i < vals.length; i++) {
var val = vals[i];
if (Math.abs(count[val] - target) > tolerance)
throw "distribution potentially skewed for " + val +
Line 258 ⟶ 259:
else
print(val + "\t" + count[val]);
}
}
 
Line 264 ⟶ 266:
}
 
try {
distcheck(function() {return Math.floor(10 * Math.random())}, 100000);
print();
distcheck(function() {return (Math.random() > 0.95 ? 1 : 0)}, 100000);</lang>
} catch (e) {
print(e);
}</lang>
 
output
<pre>0 9945
Line 279 ⟶ 286:
9 9966
 
uncaught exception: distribution potentially skewed for 0: expected result around 50000, got 95040</pre>
 
=={{header|OCaml}}==
Anonymous user