Jump to content

Verify distribution uniformity/Naive: Difference between revisions

add JavaScript
m (Fixed lang tags.)
(add JavaScript)
Line 230:
|Distribution is potentially skewed: assert
| errmsg assert(delta*expected)>|expected-{:"1 freqtable</lang>
 
=={{header|JavaScript}}==
{{trans|Tcl}}
<lang javascript>function distcheck(random_func, times, opts) {
if (opts === undefined) opts = {}
opts['delta'] = opts['delta'] || 1.5;
 
var count = {}, vals = [];
for (var i = 0; i < times; i++) {
var val = random_func();
if (! has_property(count, val)) {
count[val] = 1;
vals.push(val);
}
else
count[val] ++;
}
vals.sort(function(a,b) {return a-b});
 
var target = times / vals.length;
var tolerance = target * opts['delta'] / 100;
 
for (var val in vals)
if (Math.abs(count[val] - target) > tolerance)
throw "distribution potentially skewed for " + val +
": expected result around " + target + ", got " +count[val];
else
print(val + "\t" + count[val]);
}
 
function has_property(obj, propname) {
return typeof(obj[propname]) == "undefined" ? false : true;
}
 
distcheck(function() {return Math.floor(10 * Math.random())}, 100000);
print();
distcheck(function() {return (Math.random() > 0.95 ? 1 : 0)}, 100000);</lang>
output
<pre>0 9945
1 9862
2 9954
3 10104
4 9861
5 10140
6 10066
7 10001
8 10101
9 9966
 
uncaught exception: distribution potentially skewed for 0: expected result around 50000, got 95040</pre>
 
=={{header|OCaml}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.