Unbias a random generator: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations.
No edit summary
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations.)
Line 1,402:
 
=={{header|REXX}}==
<lang rexx>/*REXX program generates unbiased random numbers and displays the results. to terminal.*/
parse arg # R seed . /*get optional parameters from the CL. */
if #=='' | #=='",'" then #=1000 /*# the number of SAMPLES to be used.*/
if R=='' | R=='",'" then R=6 /*R the high number for the range. */
if datatype(seed\==, 'W') then call random ,,seed /*Not specified? Use for RANDOM seed. */
w=12; pad=left('',5) /*width of columnar output; indentation*/
dash='─'; @b='"biased'"; @ub='un'@b /*literals for the SAY column headers. */
say pad c('N',5) c(@b) c(@b'%') c(@ub) c(@ub"%") c('samples') /*6six colcolumn header.*/
dash=
do N=3 to R; b=0; u=0; do j=1 for #; b=b+randN(N)
b u=bu+randNunbiased(N)
end u=u+unbiased()/*j*/
end /*j*/
say pad c(N,5) c(b) pct(b) c(u) pct(u) c(#)
end /*N*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*───────────────────────────────────one─liner subroutines────────────────────*/
c: return center( arg(1), word(arg(2) w, 1), left(dash, 1) )
pct: return c( format(arg(1) / # * 100, , 2)'%' ) /*2two decimal digsdigits.*/
randN: parse arg z; return random(1, z)==z
unbiased: do until x\==randN(N); x=randN(N); end; /*until*/; return x</lang>
'''output''' &nbsp; when using the default inputs:
<pre>
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples───
Line 1,431 ⟶ 1,430:
6 178 17.80% 488 48.80% 1000
</pre>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 10000 </tt>
<pre>
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples───
Line 1,439 ⟶ 1,438:
6 1644 16.44% 4982 49.82% 10000
</pre>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 100000 &nbsp; 30 </tt>
<pre>
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples───