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 . |
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= |
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 |
say pad c('N',5) c(@b) c(@b'%') c(@ub) c(@ub"%") c('samples') /*6 col header.*/ |
||
dash= |
dash= |
||
do N=3 to |
do N=3 to R; b=0; u=0; do j=1 for # |
||
b=b+randN(N) |
|||
u=u+unbiased() |
|||
end /*j*/ |
end /*j*/ |
||
say |
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────────────────────*/ |
||
c: return center(arg(1), word(arg(2) w,1), left(dash,1)) |
|||
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 |
'''output''' when using the default inputs: |
||
<pre> |
<pre> |
||
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples─── |
|||
─N─ ────biased──── ────biased──── ───unbiased─── ───unbiased─── ───samples──── |
|||
3 348 34.80% 541 54.10% 1000 |
|||
4 259 25.90% 479 47.90% 1000 |
|||
5 188 18.80% 475 47.50% 1000 |
|||
6 178 17.80% 488 48.80% 1000 |
|||
</pre> |
</pre> |
||
'''output''' when using the |
'''output''' when using the input of: <tt> 10000 </tt> |
||
<pre> |
<pre> |
||
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples─── |
|||
─N─ ────biased──── ────biased──── ───unbiased─── ───unbiased─── ───samples──── |
|||
3 3435 34.35% 4995 49.95% 10000 |
|||
4 2535 25.35% 4957 49.57% 10000 |
|||
5 2019 20.19% 4958 49.58% 10000 |
|||
6 1644 16.44% 4982 49.82% 10000 |
|||
</pre> |
</pre> |
||
'''output''' when using the input of: <tt> |
'''output''' when using the input of: <tt> 100000 30 </tt> |
||
<pre> |
<pre> |
||
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples─── |
|||
─N─ ────biased──── ────biased──── ───unbiased─── ───unbiased─── ───samples──── |
|||
3 33301 33.30% 50066 50.07% 100000 |
|||
4 25359 25.36% 49401 49.40% 100000 |
|||
5 20026 20.03% 49966 49.97% 100000 |
|||
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> |
||