Anonymous user
Verify distribution uniformity/Naive: Difference between revisions
Verify distribution uniformity/Naive (view source)
Revision as of 16:05, 7 January 2010
, 14 years ago→{{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'] ||
var count = {}, vals = [];
Line 252:
var tolerance = target * opts['delta'] / 100;
for (var
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);
} catch (e) {
print(e);
}</lang>
output
<pre>0 9945
Line 279 ⟶ 286:
9 9966
=={{header|OCaml}}==
|