Verify distribution uniformity/Naive: Difference between revisions
Content added Content deleted
m (→{{header|Racket}}: Fixed language in template) |
(→{{header|REXX}}: added the REXX language. -- ~~~~) |
||
Line 1,075: | Line 1,075: | ||
#f |
#f |
||
</lang> |
</lang> |
||
=={{header|REXX}}== |
|||
<lang rexx>/*REXX pgm simulates a # of trials of a random digit, show it's skew %. */ |
|||
parse arg f t d s . /*obtain arguments (options). */ |
|||
if f=='' | f==',' then f='RANDOM' /* func ¬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 seed for repeatibility*/ |
|||
highDig=9 /*use this for the highest digit.*/ |
|||
!.=0 /*zero all possible random trials*/ |
|||
do j=1 for t /* [↓] perform a lot of trials. */ |
|||
if f=='RANDOM' then ?=random(0,highDig) /*random func.*/ |
|||
else interpret '?='f"(0,"highDig')' /* user func.*/ |
|||
!.?=!.?+1 /*bump counter*/ |
|||
end /*j*/ /* [↑] trials ───► pigeonholes. */ |
|||
/* [↓] compute the dig skewness.*/ |
|||
g=t/(1+highDig) /*calculate # of each digit throw*/ |
|||
OK?='OK skewed' /*words to show skewed or if OK. */ |
|||
w=max(8,length(t)) /*maximum length of # of trials. */ |
|||
pad=left('',9) /*this is used for indentation. */ |
|||
say pad 'digit' center("hits",w) ' skew ' "skew%" 'result' /*hdr. */ |
|||
say pad '─────' center('',w,'─') '──────' "─────" '──────' /*sep. */ |
|||
/** [↑] show header & separator.*/ |
|||
do k=0 to highDig /*process each of the possible #.*/ |
|||
skew=g-!.k /*calculate the skew for the dig.*/ |
|||
skewPC=(1-(g-abs(skew))/g)*100 /* " " " percentage. */ |
|||
ok=right(word(ok?,1+(skewPC>d)),6) /*it's gotta be one or the other.*/ |
|||
say pad center(k,5) right(!.k,w) right(skew,6) format(skewPC,,3) ok |
|||
end /*k*/ |
|||
say pad '─────' center('',w,'─') '──────' "─────" '──────' /*sep. */ |
|||
y=5+1+w+1+6+1+6+1+6 /*width*/ |
|||
say pad center(" (with " t ' trials)',y) /*info.*/ |
|||
say pad center(" (skewed when exceeds " d'%)',y) /*info.*/ |
|||
/*stick a fork in it, we're done.*/</lang> |
|||
'''output''' using the default inputs: |
|||
<pre> |
|||
digit hits skew skew% result |
|||
───── ──────── ────── ───── ────── |
|||
0 99790 210 0.210 OK |
|||
1 99564 436 0.436 OK |
|||
2 100061 -61 0.061 OK |
|||
3 99797 203 0.203 OK |
|||
4 99877 123 0.123 OK |
|||
5 99578 422 0.422 OK |
|||
6 99889 111 0.111 OK |
|||
7 100619 -619 0.619 skewed |
|||
8 100414 -414 0.414 OK |
|||
9 100411 -411 0.411 OK |
|||
───── ──────── ────── ───── ────── |
|||
(with 1000000 trials) |
|||
(skewed when exceeds 0.5%) |
|||
</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |