Unbias a random generator: Difference between revisions

m
→‎{{header|REXX}}: simplified program, added/changed comments and whitespace, used template for the OUTPUTs.
m (→‎{{header|REXX}}: simplified program, added/changed comments and whitespace, used template for the OUTPUTs.)
Line 1,521:
=={{header|REXX}}==
<lang rexx>/*REXX program generates unbiased random numbers and displays the results to terminal.*/
parse arg # R seed . /*getobtain optional parametersarguments 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 specifiedSpecified? UseThen use for RANDOM seed. */
wdash=12'─'; pad@b=left('',5)"biased"; @ub='un'@b /*literals for the SAY column headers. /*width of columnar output; indentation*/
say padleft('',5) cctr('N',5) c ctr(@b) c ctr(@b'"%'") cctr(@ub) cctr(@ub"'%"') c ctr('"samples'") /*six column header.*/
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') /*six column header.*/
dash=
do N=3 to R; b=0; ub=0; do ju=1 for #; b=b+randN(N)0
do j=1 for #; b=b + randN(N); u=u + unbiased()
end /*j*/
say pad left('', c5) ctr(N, 5) c ctr(b) pct(b) c ctr(u) pct(u) c ctr(#)
end /*N*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
cctr: return center( arg(1), word(arg(2) w12, 1), left(dash, 1) ) /*show hdr│numbers.*/
pct: return cctr( format(arg(1) / # * 100, , 2)'%' ) /*two2 decimal digits.*/
randN: parse arg z; return random(1, z)==z /*ret return random(1, z)if rand==zZ.*/
unbiased: do until x\==randN(N); x=randN(N); end /*until*/; return x /* " unbiased return xRAND*/</lang>
'''{{out|output''' |text=&nbsp; when using the default inputs:}}
<pre>
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples───
Line 1,548 ⟶ 1,547:
6 178 17.80% 488 48.80% 1000
</pre>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; <tt> 10000 </tt>}}
<pre>
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples───
Line 1,556 ⟶ 1,555:
6 1644 16.44% 4982 49.82% 10000
</pre>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; <tt> 100000 &nbsp; 30 </tt>}}
<pre>
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples───