Verify distribution uniformity/Naive: Difference between revisions

m
→‎{{header|REXX}}: changed/added whitespace and comments, centered the result.
(Added Elixir)
m (→‎{{header|REXX}}: changed/added whitespace and comments, centered the result.)
Line 1,202:
 
=={{header|REXX}}==
<lang rexx>/*REXX pgm simulates a #number of trials of a random digit, and show it's skew %. */
parse arg f t d s . /*obtain arguments (options). from C.L. */
if f=='' | f==',' then f='RANDOM' /*function funcnot ¬specified? Use default.*/
if t=='' | t==',' then t=1000000 /*times " " " " */
if d=='' | d==',' then d=1/2 /*delta% " " " " */
if s\=='' then call random ,,s /*use some RAND seed for repeatibilityrepeatability.*/
highDig=9 /*use this var for the highest digit. */
!.=0 /*zeroinitialize all possible random trials*/
do j=1 for t /* [↓] perform a lotbunch of trials. */
if f=='RANDOM' then ?=random(0,highDig) /*random funcfunction.*/
else interpret '?='f"(0,"highDig')' /* user funcfunction.*/
!.?=!.?+1 /*bump the counter*/
end /*j*/ /* [↑] store trials ───► pigeonholes. */
/* [↓] compute the digdigit's skewness. */
g=t/(1+highDig) /*calculate #number of each digit throw.*/
OK?='OK skewed' /*words to show "skewed" or if "OK". */
w=max(8,length(t)) /*maximum length of # number of trials. */
pad=left('',9) /*this is used for output indentation. */
say pad 'digit' center("hits",w) ' skew ' "skew%" 'result' /*hdrheader. */
say pad '─────' center('',w,'─') '──────' "─────" '──────' /*sepseparator. */
/** [↑] show header &and the separator.*/
do k=0 to highDig /*process each of the possible #digits. */
skew=g-!.k /*calculate the skew for the digdigit. */
skewPC=(1-(g-abs(skew))/g)*100 /* " " " percentage. for dig*/
ok=rightcenter(word(ok?,1+(skewPC>d)),6) /*it's gotta be one of skewed or the other.xx%*/
say pad center(k,5) right(!.k,w) right(skew,6) format(skewPC,,3) ok
end /*k*/
 
say pad '─────' center('',w,'─') '──────' "─────" '──────' /*sepseparator. */
y=5+1+w+1+6+1+6+1+6 /*the width. */
say pad center(" (with " t ' trials)',y) /*info# trials. */
say pad center(" (skewed when exceeds " d'%)',y) /*info.skewed note*/
/*stick a fork in it, we're all done. */</lang>
Execution note: &nbsp; quite a few runs were needed and the skew% lowered before a skewed result was obtained.
<br><br>
'''output''' when using the default inputs:
<pre>
digit hits skew skew% result
───── ──────── ────── ───── ──────
0 9979099757 210243 0.210 243 OK
1 100226 99564 436-226 0.436 226 OK
2 100061 100605 -61605 0.061 605 OKskewed
3 100005 99797 203-5 0.203 005 OK
4 9987799670 123330 0.123 330 OK
5 100011 99578 422-11 0.422 011 OK
6 100100 99889 111-100 0.111 100 OK
7 100619 99513 -619 487 0.619487 skewedOK
8 100414 99884 -414 0.414 116 0.116 OK
9 100411100229 -411229 0.411 229 OK
───── ──────── ────── ───── ──────
(with 1000000 trials)