Unbias a random generator: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: used literal variables instead of literals, added/changed whitespace and comments.)
(→‎{{header|REXX}}: added support for the RANGE of the tallies, added indentation, changed some column headers to include %.)
Line 1,329: Line 1,329:
=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program generates unbiased random numbers and displays the results. */
<lang rexx>/*REXX program generates unbiased random numbers and displays the results. */
parse arg # seed . /*get optional parameters from the CL. */
parse arg # R seed . /*get optional parameters from the CL. */
if #=='' | #==',' then #=1000 /*# the number of SAMPLES to be used.*/
if #=='' | #==',' then #=1000 /*# the number of SAMPLES to be used.*/
if R=='' | R==',' then R=6 /*R the high number for the range. */
if seed\=='' then call random ,,seed /*Not specified? Use for RANDOM seed. */
if seed\=='' then call random ,,seed /*Not specified? Use for RANDOM seed. */
w=14 /*width of the columnar output for SAY.*/
w=12; pad=left('',5) /*width of columnar output; indentation*/
dash='─'; @b='biased'; @ub='un'@b /*literals for the SAY column headers. */
dash='─'; @b='biased'; @ub='un'@b /*literals for the SAY column headers. */
say ct('N',3) ct(@b) ct(@b) ct(@ub) ct(@ub) ct('samples') /*6 column header.*/
say pad c('N',5) c(@b) c(@b'%') c(@ub) c(@ub"%") c('samples') /*6 col header.*/
dash=
dash=
do N=3 to 6; b=0; u=0; do j=1 for #
do N=3 to R; b=0; u=0; do j=1 for #
b = b + randN(N)
b=b+randN(N)
u = u + unbiased()
u=u+unbiased()
end /*j*/
end /*j*/
say ct(N,3) ct(b) pc(b) ct(u) pc(u) ct(#)
say pad c(N,5) c(b) pct(b) c(u) pct(u) c(#)
end /*N*/
end /*N*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*───────────────────────────────────one─liner subroutines────────────────────*/
/*───────────────────────────────────one─liner subroutines────────────────────*/
ct: return center(arg(1), word(arg(2) w,1), left(dash,1))
c: return center(arg(1), word(arg(2) w,1), left(dash,1))
pc: return ct(format(arg(1)/#*100,,2)'%') /*2 decimal digs.*/
pct: return c(format(arg(1)/#*100,,2)'%') /*2 decimal digs.*/
randN: parse arg z; return random(1,z)==z
randN: parse arg z; return random(1,z)==z
unbiased: do until x\==randN(N); x=randN(N); end; return x</lang>
unbiased: do until x\==randN(N); x=randN(N); end; return x</lang>
'''output''' when using the input of: &nbsp; <tt> 100 </tt>
'''output''' when using the default inputs:
<pre>
<pre>
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples───
─N─ ────biased──── ────biased──── ───unbiased─── ───unbiased─── ───samples────
3 31 31.00% 50 50.00% 100
3 348 34.80% 541 54.10% 1000
4 23 23.00% 45 45.00% 100
4 259 25.90% 479 47.90% 1000
5 20 20.00% 51 51.00% 100
5 188 18.80% 475 47.50% 1000
6 16 16.00% 51 51.00% 100
6 178 17.80% 488 48.80% 1000
</pre>
</pre>
'''output''' when using the default input of: &nbsp; <tt> 1000 </tt>
'''output''' when using the input of: &nbsp; <tt> 10000 </tt>
<pre>
<pre>
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples───
─N─ ────biased──── ────biased──── ───unbiased─── ───unbiased─── ───samples────
3 317 31.70% 484 48.40% 1000
3 3435 34.35% 4995 49.95% 10000
4 245 24.50% 481 48.10% 1000
4 2535 25.35% 4957 49.57% 10000
5 211 21.10% 481 48.10% 1000
5 2019 20.19% 4958 49.58% 10000
6 164 16.40% 493 49.30% 1000
6 1644 16.44% 4982 49.82% 10000
</pre>
</pre>
'''output''' when using the input of: &nbsp; <tt> 10000 </tt>
'''output''' when using the input of: &nbsp; <tt> 100000 &nbsp; 30 </tt>
<pre>
<pre>
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples───
─N─ ────biased──── ────biased──── ───unbiased─── ───unbiased─── ───samples────
3 3366 33.66% 5023 50.23% 10000
3 33301 33.30% 50066 50.07% 100000
4 2524 25.24% 4950 49.50% 10000
4 25359 25.36% 49401 49.40% 100000
5 1940 19.40% 4981 49.81% 10000
5 20026 20.03% 49966 49.97% 100000
6 1693 16.93% 5034 50.34% 10000
6 16579 16.58% 49956 49.96% 100000
7 14294 14.29% 50008 50.01% 100000
8 12402 12.40% 50479 50.48% 100000
9 11138 11.14% 50099 50.10% 100000
10 9973 9.97% 49988 49.99% 100000
11 9062 9.06% 50009 50.01% 100000
12 8270 8.27% 49929 49.93% 100000
13 7704 7.70% 49876 49.88% 100000
14 7223 7.22% 50414 50.41% 100000
15 6725 6.73% 50043 50.04% 100000
16 6348 6.35% 50252 50.25% 100000
17 5900 5.90% 49977 49.98% 100000
18 5583 5.58% 49991 49.99% 100000
19 5139 5.14% 49958 49.96% 100000
20 4913 4.91% 50198 50.20% 100000
21 4714 4.71% 49892 49.89% 100000
22 4517 4.52% 49760 49.76% 100000
23 4226 4.23% 50021 50.02% 100000
24 4174 4.17% 50141 50.14% 100000
25 4005 4.01% 49816 49.82% 100000
26 3890 3.89% 49819 49.82% 100000
27 3705 3.71% 50036 50.04% 100000
28 3567 3.57% 49665 49.67% 100000
29 3481 3.48% 50094 50.09% 100000
30 3355 3.36% 49831 49.83% 100000
</pre>
</pre>