Verify distribution uniformity/Naive: Difference between revisions

Add J
(→‎{{header|C++}}: another bug)
(Add J)
Line 168:
NIL
#<EQL Hash Table{7} 200CB5BB></pre>
 
=={{header|J}}==
This solution defines the checker as an adverb. Adverbs combine with the verb immediately to their left to create a new verb. So for a verb <code>generateDistribution</code> and an adverb <code>checkUniform</code>, the new verb might be thought of as <code>checkGeneratedDistributionisUniform</code>.
 
The ''delta'' is given as an optional left argument (<code>x</code>), defaulting to 5%. The right argument (<code>y</code>) is any valid argument to the distribution generating verb.
<lang j>
checkUniform=: adverb define
0.05 u checkUniform y
:
n=. */y
delta=. x
sample=. u n NB. the u refers to the verb given to the left of the adverb
freqtable=. /:~ (~. ,. #/.~) sample
expected=. n % # freqtable
'lbound ubound'=. expected * 1 (- , +) delta
errmsg=. 'Distribution is potentially skewed'
errmsg assert (lbound&< *. ubound&>) {:"1 freqtable
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 isUniform 1e5
1 14082
2 14337
3 14242
4 14470
5 14067
6 14428
7 14374
0.05 rollD7t isUniform 1e2
|Distribution is potentially skewed: assert
| errmsg assert(lbound&<*.ubound&>){:"1 freqtable
</lang>
 
=={{header|OCaml}}==
892

edits