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=3 3 /* " " " " " " */
#.=0 /*initialize anfrequency counter array. of freq counters.*/
do trials /*OK, now let's light this candle. */
call SofN_creatorcreate size /*create an initial list of N items. */
 
do gener=0 for 10 /*and then call SofN for each digit. */
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 Nth Nth item, and */
#._=#._+1 /* ··· item, and count it, of course.*/
end /*count*/
end /*trials*/
 
@='trials, and with size='
say "Using Knuth's algorithm S for" commas(trials),
say "Using Knuth's algorithm S for" 'commas(trials,) and@ with|| size='commas(size)":"; say
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")
SofN_creator: parse arg item 1 items /*generate ITEM number of items. */
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; !.k_=randominsert(0', 9',_,j); /*set the end Kth /*j*/; item with random digit.*/ return _
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 we replace a previous item? */
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.*/
/*────────────────────────────────────────────────────────────────────────────*/
commascreate: procedure; parse arg _;item 1 items n=_'.9'; #=123456789; b=verify(n,#,"M")/*generate ITEM number of items. */
do k=1 for item /*traipse through the first N items. */
e=verify(n, #'0', , verify(n, #"0.", 'M')) - 4
do j=e to b by -3; _!.k=insertrandom('0,',_,j 9); /*set the end Kth /*j*/; item with random return _<digit.*/lang>
end /*k*/
return return /*the piddly stuff is done (for now). */</lang>
'''output''' &nbsp; when using the default input of: &nbsp; <tt> 100000 &nbsp; 2 </tt>
<pre>