Verify distribution uniformity/Naive: Difference between revisions
Content added Content deleted
(Added Elixir) |
m (→{{header|REXX}}: changed/added whitespace and comments, centered the result.) |
||
Line 1,202: | Line 1,202: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX pgm simulates a |
<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) |
parse arg f t d s . /*obtain arguments (options) from C.L. */ |
||
if f=='' | f==',' then f='RANDOM' /* |
if f=='' | f==',' then f='RANDOM' /*function not specified? Use default.*/ |
||
if t=='' | t==',' then t=1000000 /*times " " " */ |
if t=='' | t==',' then t=1000000 /*times " " " " */ |
||
if d=='' | d==',' then d=1/2 /*delta% " " " */ |
if d=='' | d==',' then d=1/2 /*delta% " " " " */ |
||
if s\=='' then call random ,,s /*use some seed for |
if s\=='' then call random ,,s /*use some RAND seed for repeatability.*/ |
||
highDig=9 /*use this for the highest digit.*/ |
highDig=9 /*use this var for the highest digit. */ |
||
!.=0 /* |
!.=0 /*initialize all possible random trials*/ |
||
do j=1 for t /* [↓] perform a |
do j=1 for t /* [↓] perform a bunch of trials. */ |
||
if f=='RANDOM' then ?=random(0,highDig) /*random |
if f=='RANDOM' then ?=random(0,highDig) /*random function.*/ |
||
else interpret '?='f"(0,"highDig')' /* user |
else interpret '?='f"(0,"highDig')' /* user function.*/ |
||
!.?=!.?+1 /*bump counter*/ |
!.?=!.?+1 /*bump the counter*/ |
||
end /*j*/ /* [↑] trials ───► pigeonholes. */ |
end /*j*/ /* [↑] store trials ───► pigeonholes. */ |
||
/* [↓] compute the |
/* [↓] compute the digit's skewness. */ |
||
g=t/(1+highDig) /*calculate |
g=t/(1+highDig) /*calculate number of each digit throw.*/ |
||
OK?='OK skewed' /*words to show skewed or if OK. |
OK?='OK skewed' /*words to show "skewed" or if "OK".*/ |
||
w=max(8,length(t)) /*maximum length of |
w=max(8,length(t)) /*maximum length of number of trials.*/ |
||
pad=left('',9) /*this is used for indentation. |
pad=left('',9) /*this is used for output indentation. */ |
||
say pad 'digit' center("hits",w) ' skew ' "skew%" 'result' /* |
say pad 'digit' center("hits",w) ' skew ' "skew%" 'result' /*header. */ |
||
say pad '─────' center('',w,'─') '──────' "─────" '──────' /* |
say pad '─────' center('',w,'─') '──────' "─────" '──────' /*separator.*/ |
||
/** [↑] show header |
/** [↑] show header and the separator.*/ |
||
do k=0 to highDig /*process each of the possible |
do k=0 to highDig /*process each of the possible digits. */ |
||
skew=g-!.k /*calculate the skew for the |
skew=g-!.k /*calculate the skew for the digit. */ |
||
skewPC=(1-(g-abs(skew))/g)*100 /* " " " percentage |
skewPC=(1-(g-abs(skew))/g)*100 /* " " " percentage for dig*/ |
||
ok= |
ok=center(word(ok?,1+(skewPC>d)),6) /*it's gotta be one of skewed or xx%*/ |
||
say pad center(k,5) right(!.k,w) right(skew,6) format(skewPC,,3) ok |
say pad center(k,5) right(!.k,w) right(skew,6) format(skewPC,,3) ok |
||
end /*k*/ |
end /*k*/ |
||
say pad '─────' center('',w,'─') '──────' |
say pad '─────' center('',w,'─') '──────' "─────" '──────' /*separator. */ |
||
y=5+1+w+1+6+1+6+1+6 /*width*/ |
y=5+1+w+1+6+1+6+1+6 /*the width. */ |
||
say pad center(" (with " t ' trials)',y) |
say pad center(" (with " t ' trials)',y) /*# trials. */ |
||
say pad center(" (skewed when exceeds " d'%)',y) /* |
say pad center(" (skewed when exceeds " d'%)',y) /*skewed note*/ |
||
/*stick a fork in it, we're done.*/</lang> |
/*stick a fork in it, we're all done. */</lang> |
||
Execution note: quite a few runs were needed and the skew% lowered before a skewed result was obtained. |
Execution note: quite a few runs were needed and the skew% lowered before a skewed result was obtained. |
||
<br><br> |
<br><br> |
||
'''output''' using the default inputs: |
'''output''' when using the default inputs: |
||
<pre> |
<pre> |
||
digit hits skew skew% result |
digit hits skew skew% result |
||
───── ──────── ────── ───── ────── |
───── ──────── ────── ───── ────── |
||
0 |
0 99757 243 0.243 OK |
||
1 |
1 100226 -226 0.226 OK |
||
2 |
2 100605 -605 0.605 skewed |
||
3 |
3 100005 -5 0.005 OK |
||
4 |
4 99670 330 0.330 OK |
||
5 |
5 100011 -11 0.011 OK |
||
6 |
6 100100 -100 0.100 OK |
||
7 |
7 99513 487 0.487 OK |
||
8 |
8 99884 116 0.116 OK |
||
9 |
9 100229 -229 0.229 OK |
||
───── ──────── ────── ───── ────── |
───── ──────── ────── ───── ────── |
||
(with 1000000 trials) |
(with 1000000 trials) |