Probabilistic choice: Difference between revisions

Content added Content deleted
(Added Wren)
m (→‎{{header|REXX}}: changed comments and whitespace.)
Line 2,692: Line 2,692:


=={{header|REXX}}==
=={{header|REXX}}==
Note:   REXX can generate random numbers up to   100,000.
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.*/
<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*/
parse arg trials digs seed . /*obtain the optional arguments from CL*/
if trials=='' | trials=="," then trials=1000000 /*Not specified? Then use the default.*/
if trials=='' | trials=="," then trials= +1e6 /*Not specified? Then use the default.*/
if digs=='' | digs=="," then digs=15 /* " " " " " " */
if digs=='' | digs=="," then digs= 15 /* " " " " " " */
if datatype(seed, 'W') then call random ,,seed /*allows repeatability for RANDOM nums.*/
if datatype(seed, 'W') then call random ,,seed /*allows repeatability for RANDOM nums.*/
numeric digits digs /*use a specific number of decimal digs*/
numeric digits digs /*use a specific number of decimal digs*/
names= 'aleph beth gimel daleth he waw zayin heth ───totals───►' /*names of the cells.*/
names= 'aleph beth gimel daleth he waw zayin heth ───totals───►' /*names of the cells.*/
HI=100000 /*max REXX RANDOM num*/
hi= 100000 /*max REXX RANDOM num*/
z=words(names); #=z - 1 /*#≡the number of actual/useable names.*/
z= words(names); #= z - 1 /*# ≡the number of actual/usable names.*/
$=0 /*initialize sum of the probabilities. */
$= 0 /*initialize sum of the probabilities. */
do n=1 for #; prob.n=1 / (n+4); if n==# then prob.n= 1759 / 27720
do n=1 for #; prob.n= 1 / (n+4); if n==# then prob.n= 1759 / 27720
$=$ + prob.n; Hprob.n=prob.n * HI
$= $ + prob.n; Hprob.n= prob.n * hi /*spread the range of probabilities. */
end /*n*/
end /*n*/
prob.z=$ /*define the value of the ───totals───.*/
prob.z= $ /*define the value of the ───totals───.*/
@.=0 /*initialize all counters in the range.*/
@.= 0 /*initialize all counters in the range.*/
@.z=trials /*define the last counter of " " */
@.z= trials /*define the last counter of " " */
do j=1 for trials; r=random(HI) /*gen TRIAL number of random numbers.*/
do j=1 for trials; r= random(hi) /*gen TRIAL number of random numbers.*/
do k=1 for # /*for each cell, compute percentages. */
do k=1 for # /*for each cell, compute percentages. */
if r<=Hprob.k then @.k=@.k + 1 /* " " " range, bump the counter*/
if r<=Hprob.k then @.k= @.k + 1 /* " " " range, bump the counter*/
end /*k*/
end /*k*/
end /*j*/
end /*j*/
_= '═' /*_: a literal used for CENTER BIF pad*/
_= '═' /*_: padding used by the CENTER BIF.*/
w=digs + 6 /*W: display width for the percentages*/
w= digs + 6 /*W: display width for the percentages*/
d=4 + max( length(trials), length('count') ) /* [↓] display a formatted top header.*/
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,_)
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.*/
do cell=1 for z /*display each of the cells and totals.*/
say ' ' left( word(names, cell), 13) right(@.cell, d-2) " ",
say ' ' left( word(names, cell), 13) right(@.cell, d-2) " ",
left( format( prob.cell * 100, d), w-2),
left( format( prob.cell * 100, d), w-2),
left( format( @.cell/trials * 100, d), w-2)
left( format( @.cell/trials * 100, d), w-2)