Anonymous user
Knuth's algorithm S: Difference between revisions
m
→{{header|REXX}}: changed comments and whitespace, used a simpler DO loop index, changed header/title width.
m (→{{header|REXX}}: changed comments and whitespace, used a simpler DO loop index, changed header/title width.) |
|||
Line 1,496:
=={{header|REXX}}==
<lang rexx>/*REXX program using Knuth's algorithm S (a random sampling N of M items).
parse arg trials size . /*obtain optional arguments from the CL*/
if trials=='' | trials=="," then trials=
if size=='' | size=="," then size=
#.=
do trials /*OK, now let's light this candle. */
call s_of_n_creator size /*create an initial list of N items. */
do
▲ end /*gener*/
/* [↓] examine what SofN generated. */
do count=1 for size; _= !.count /*get a dec. digit from the Nth item. */
#._= #._ + 1
end /*count*/
end /*trials*/
@= ' trials, and with a size of '
hdr= " Using Knuth's algorithm S for " commas(trials) @ || commas(size)": "
say hdr;
do dig=0 to 9 /* [↓] display the frequency of a dig.*/
Line 1,524 ⟶ 1,522:
commas: parse arg _; do jc=length(_)-3 to 1 by -3; _=insert(',', _, jc); end; return _
/*──────────────────────────────────────────────────────────────────────────────────────*/
s_of_n: parse arg item;
return /* ··· item to replace with ITEM.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
s_of_n_creator: parse arg item 1 items /*generate ITEM number of items. */
do k=1 for item /*traipse through the first N items. */
!.k= random(0, 9)
end /*k*/
{{out|output|text= when using the default input of: <tt> 100000 2 </tt>}}
<pre>
═══════════════════════════════════════════════════════════════════════════
frequency of the 0 digit is: 29,
frequency of the 1 digit is:
frequency of the 2 digit is: 30,
frequency of the 3 digit is:
frequency of the 4 digit is:
frequency of the 5 digit is:
frequency of the 6 digit is:
frequency of the 7 digit is: 30,
frequency of the 8 digit is:
frequency of the 9 digit is:
</pre>
|