Jump to content

Unbias a random generator: Difference between revisions

→‎{{header|REXX}}: added support for the RANGE of the tallies, added indentation, changed some column headers to include %.
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:
=={{header|REXX}}==
<lang rexx>/*REXX program generates unbiased random numbers and displays the results. */
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 seed\=='' then call random ,,seed /*Not specified? Use for RANDOM seed. */
w=14 12; pad=left('',5) /*width of the columnar output; for SAY.indentation*/
dash='─'; @b='biased'; @ub='un'@b /*literals for the SAY column headers. */
say ctpad c('N',35) ctc(@b) ctc(@b'%') ctc(@ub) ctc(@ub"%") ctc('samples') /*6 columncol header.*/
dash=
do N=3 to 6R; b=0; u=0; do j=1 for #
b = b + b=b+randN(N)
u = u + u=u+unbiased()
end /*j*/
say ctpad c(N,35) ctc(b) pcpct(b) ctc(u) pcpct(u) ctc(#)
end /*N*/
exit /*stick a fork in it, we're all done. */
/*───────────────────────────────────one─liner subroutines────────────────────*/
ctc: return center(arg(1), word(arg(2) w,1), left(dash,1))
pcpct: return ctc(format(arg(1)/#*100,,2)'%') /*2 decimal digs.*/
randN: parse arg z; return random(1,z)==z
unbiased: do until x\==randN(N); x=randN(N); end; return x</lang>
'''output''' when using the inputdefault ofinputs: &nbsp; <tt> 100 </tt>
<pre>
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples───
─N─ ────biased──── ────biased──── ───unbiased─── ───unbiased─── ───samples────
3 313 348 31.00% 34.80% 50 541 5054.0010% 1001000
4 234 259 23.00% 25.90% 45 479 4547.0090% 1001000
5 205 188 20.00% 18.80% 51 475 5147.0050% 1001000
6 166 178 16.00% 17.80% 51 488 5148.0080% 1001000
</pre>
'''output''' when using the default input of: &nbsp; <tt> 100010000 </tt>
<pre>
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples───
─N─ ────biased──── ────biased──── ───unbiased─── ───unbiased─── ───samples────
3 317 3 3435 31.70% 34.35% 484 4995 4849.4095% 100010000
4 245 4 2535 24.50% 25.35% 481 4957 4849.1057% 100010000
5 211 5 2019 21.10% 20.19% 481 4958 4849.1058% 100010000
6 164 6 1644 16.40% 16.44% 493 4982 49.3082% 100010000
</pre>
'''output''' when using the input of: &nbsp; <tt> 10000100000 &nbsp; 30 </tt>
<pre>
──N── ───biased─── ──biased%─── ──unbiased── ─unbiased%── ──samples───
─N─ ────biased──── ────biased──── ───unbiased─── ───unbiased─── ───samples────
3 3366 3 33301 33.66% 33.30% 5023 50066 50.2307% 10000100000
4 2524 4 25359 25.24% 25.36% 4950 49401 49.5040% 10000100000
5 1940 5 20026 19.40% 20.03% 4981 49966 49.8197% 10000100000
6 1693 6 16579 16.93% 16.58% 5034 49956 5049.3496% 10000100000
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>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.