Probabilistic choice: Difference between revisions

m
→‎{{header|REXX}}: changed comments and whitespace.
(Added Wren)
m (→‎{{header|REXX}}: changed comments and whitespace.)
Line 2,692:
 
=={{header|REXX}}==
Note:   REXX can generate random numbers up to a range of   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*/
if trials=='' | trials=="," then trials=1000000 +1e6 /*Not specified? Then use the default.*/
if digs=='' | digs=="," then digs=15 15 /* " " " " " " */
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.*/
HIhi=100000 100000 /*max REXX RANDOM num*/
z= words(names); #=z - 1 #= z - 1 /*# ≡the number of actual/useableusable names.*/
$=0 0 /*initialize sum of the probabilities. */
do n=1 for #; prob.n= 1 / (n+4); if n==# then prob.n= 1759 / 27720
$= $ + prob.n; Hprob.n= prob.n * HIhi /*spread the range of probabilities. */
end /*n*/
prob.z=$ $ /*define the value of the ───totals───.*/
@.=0 0 /*initialize all counters in the range.*/
@.z=trials trials /*define the last counter of " " */
do j=1 for trials; r= random(HIhi) /*gen TRIAL 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 literalpadding used forby the CENTER BIF padBIF.*/
w= digs + 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 cell=1 for z /*display each of the cells and totals.*/
say ' ' left( word(names, cell), 13) right(@.cell, d-2) " ",
left( format( prob.cell * 100, d), w-2),
left( format( @.cell/trials * 100, d), w-2)