Anonymous user
Knuth's algorithm S: Difference between revisions
m
→{{header|REXX}}: added/changed whitespace and comments, ordered the subroutines/functions, broke a long statement into smaller ones.
m (→{{header|REXX}}: added/changed whitespace and comments, ordered the subroutines/functions, broke a long statement into smaller ones.) |
|||
Line 1,242:
parse arg trials size . /*obtain optional arguments from the CL*/
if trials=='' then trials=100000 /*Not specified? Then use the default.*/
if size=='' then size=
#.=0 /*initialize
do trials /*OK, now let's light this candle. */
call
do gener=0 for 10
call SofN gener /*call SofN with a single decimal dig*/
end /*gener*/
do count=1 for size /*let's examine what SofN generated. */
_=!.count /*get a decimal digit from the
#._=#._+1 /*
end /*count*/
end /*trials*/
@='trials, and with size='
say "Using Knuth's algorithm S for"
say
do dig=0 to 9 /* [↓] display the frequency of a dig.*/
say left('',20) "frequency of the" dig 'digit is:' commas(#.dig)
Line 1,264 ⟶ 1,265:
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
commas: procedure; parse arg _; n=_'.9'; #=123456789; b=verify(n,#,"M")
e=verify(n, #'0', , verify(n, #"0.", 'M')) - 4▼
do k=1 for item /*traipse through the first N items. */▼
do j=e to b by -3;
end /*k*/▼
return /*the piddly stuff is done for now. */▼
/*────────────────────────────────────────────────────────────────────────────*/
SofN: parse arg item; items=items+1 /*get "item", bump the items counter.*/
c=random(1, items) /* [↓] should
if c>size then return /*probability isn't good, so skip it. */
_=random(1, size); !._=item /*now, figure out which previous ··· */
return /* ··· item to replace with ITEM.*/
/*────────────────────────────────────────────────────────────────────────────*/
▲ do k=1 for item /*traipse through the first N items. */
▲ e=verify(n, #'0', , verify(n, #"0.", 'M')) - 4
▲ end /*k*/
'''output''' when using the default input of: <tt> 100000 2 </tt>
<pre>
|