Verify distribution uniformity/Naive: Difference between revisions

m
whitespace
(→‎{{header|VBScript}}: Added new VBScript section)
m (whitespace)
Line 20:
=={{header|AutoHotkey}}==
<lang AutoHotkey>MsgBox, % DistCheck("dice7",10000,3)
 
 
DistCheck(function, repetitions, delta)
Line 113 ⟶ 112:
 
=={{header|C++}}==
 
<lang cpp>#include <map>
#include <iostream>
Line 148 ⟶ 146:
=={{header|Clojure}}==
The code could be shortened if the verify function did the output itself, but the "Clojure way" is to have functions, whenever possible, avoid side effects (like printing) and just return a result. Then the "application-level" code uses doseq and println to display the output to the user. The built-in (rand-int) function is used for all three runs of the verify function, but first with small N to simulate experimental error in a small sample size, then with larger N to show it working properly on large N.
<lang clojure>(defn verify [rand n & [delta]]
(defn verify [rand n & [delta]]
(let [rands (frequencies (repeatedly n rand))
avg (/ (reduce + (map val rands)) (count rands))
Line 186 ⟶ 183:
=={{header|Common Lisp}}==
{{trans|OCaml}}
 
<lang lisp>(defun check-distribution (function n &optional (delta 1.0))
(let ((bins (make-hash-table)))
Line 210 ⟶ 206:
 
=={{header|D}}==
 
<lang d>import std.math: abs;
import std.string: format;
Line 286 ⟶ 281:
end subroutine</lang>
 
=={{header|Go}}==
<lang go>package main
Line 373 ⟶ 369:
 
=={{header|Icon}} and {{header|Unicon}}==
 
This example assumes the random number generator is passed to <code>verify_uniform</code> as a co-expression. The co-expression <code>rnd</code> is prompted for its next value using <code>@rnd</code>. The co-expression is created using <code>create (|?10)</code> where the vertical bar means generate an infinite sequence of what is to its right (in this case, <code>?10</code> generates a random integer in the range [1,10]).
<lang Icon># rnd : a co-expression, which will generate the random numbers
 
<lang Icon>
# rnd : a co-expression, which will generate the random numbers
# n : the number of numbers to test
# delta: tolerance in non-uniformity
Line 407 ⟶ 400:
then write ("uniform")
else write ("skewed")
end</lang>
</lang>
 
Output:
<pre>
Line 452 ⟶ 443:
freqtable
)</lang>
 
It is possible to use tacit expressions within an explicit definition enabling a more functional and concise style:
<lang j>checkUniformT=: adverb define
Line 462 ⟶ 452:
freqtable
)</lang>
 
Show usage using <code>rollD7t</code> given in [[Seven-dice from Five-dice#J|Seven-dice from Five-dice]]:
 
<lang j> 0.05 rollD7t checkUniform 1e5
1 14082
Line 519 ⟶ 507:
print(e);
}</lang>
Output:
 
output
<pre>0 9945
1 9862
Line 604 ⟶ 591:
test(dice7, 10^5)
test(()->if(random(1000),random(1000),1), 10^5)</lang>
 
Output:
<pre>Flat with significance 1.000000000000000000
 
### user error: Not flat enough, significance only 5.391077606003910233 E-3500006</pre>
 
=={{header|Perl 6}}==
Since the tested function is rolls of a 7 sided die, the test numbers are magnitudes of 10<sup>x</sup> bumped up to the closest multiple of 7. This reduces spurious error from there not being an integer expected value.
Line 635 ⟶ 622:
}
say '';
}</lang Icon>
}
</lang>
Sample output:
<pre>
Anonymous user