Probabilistic choice: Difference between revisions

→‎{{header|REXX}}: added/changed comments and whitespace, used a template for the output, added a comment to the REXX section header, optimized the generating of the trials.
m (→‎{{header|Ring}}: flagged as needing improvement.)
(→‎{{header|REXX}}: added/changed comments and whitespace, used a template for the output, added a comment to the REXX section header, optimized the generating of the trials.)
Line 2,690:
 
=={{header|REXX}}==
Note:   REXX can generate random numbers up to   100,000.
<lang rexx>/*REXX program displays results of probabilistic choices, gen random #s per probability.*/
parse arg trials digs seed . /*obtain the optional arguments from CL*/
Line 2,696 ⟶ 2,697:
if datatype(seed, 'W') then call random ,,seed /*allows repeatability for RANDOM nums.*/
numeric digits digs /*use a specific number of decimal digs*/
names= 'aleph beth gimel daleth he waw zayin heth ───totals───►' /*names of the cells.*/
#HI=100000 words(names) - 1; /*max REXX RANDOM s=0num*/
z=words(names); #=z - 1 /*#≡the number of actual/useable names.*/
HI=100000
$=0 do n=1 for #-1; prob.n=1 / (n+4); Hprob.n=prob.n * HI; s=s + prob /*initialize sum of the probabilities.n */
do n=1 for #; prob.n=1 / (n+4); if n==# then prob.n= 1759 / 27720
$=$ + prob.n; Hprob.n=prob.n * HI
end /*n*/
prob.z=$ /*define the value of the ───totals───.*/
!.=0
prob@.#=1759/27720;0 !.9=trials; Hprob.#=prob.# * HI; s=s + prob /*initialize all counters in the range.#*/
@.z=trials /*define the last counter of " " */
prob.9=s
do j=1 for trials; r=random(1, HI) /*generategen TRIAL X number of random numbers.*/
do k=1 for # /*for each cell, compute percentages. */
if r<=Hprob.k then !@.k=!@.k + 1 /* " " " range, bump the counter*/
end /*k*/
end /*j*/
@_= '═' /*@_: a literal used for CENTER BIF pad*/
w=digs +6 6 /*W: display width for the percentages*/
d=4 + max( length(trials), length('count') ) /* [↓] display a formatted top header.*/
say center('name',15,@_) center('count',d,@_) center('target %',w,@_) center('actual %',w,@_)
 
do icell=1 for #+1 z /*display each of the cells and totals.*/
say ' ' left( word(names, icell), 13) right(!@.icell, d-2) " ",
left( format( prob.icell * 100, d), w-2),
left( format( !@.icell/trials * 100, d), w-2)
if icell==# then say center(@_,15,@_) center(@_,d,@_) center(@_,w,@_) center(@_,w,@_)
end /*ic*/ /*stick a[↑] fork indisplay it,a formatted wefoot are all done.header*/</lang>
/*stick a fork in it, we are all done.*/</lang>
'''{{out|output''' |text=&nbsp; when using the default input:}}
<pre>
═════name══════ ═══count═══ ══════target %═══════ ══════actual %═══════
aleph 200306200135 20 20.03060135
beth 166935166912 16.6666666 16.69356912
gimel 143092143222 14.2857142 14.30923222
daleth 124951124991 12.5 12.49514991
he 110969111259 11.1111111 11.09691259
waw 99698100049 10 910.96980049
zayin 9043890978 9.0909090 9.04380978
heth 6319163278 6.3455988 6.31913278
═══════════════ ═══════════ ═════════════════════ ═════════════════════
───totals───► 1000000 100 100