Knuth's algorithm S: Difference between revisions

m
→‎{{header|Raku}}: dispense with intermediate vars, compact code layout
m (syntax highlighting fixup automation)
m (→‎{{header|Raku}}: dispense with intermediate vars, compact code layout)
Line 1,647:
(formerly Perl 6)
<syntaxhighlight lang="raku" line>sub s_of_n_creator($n) {
my (@sample, $i);
my $i = 0;
-> $item {
if ++$i <= $n { @sample.push: $item }
elsif $i.rand < push$n { @sample,[$n.rand] = $item; }
}@sample
elsif $i.rand < $n {
@sample[$n.rand] = $item;
}
@sample;
}
}
 
my @items = 0..9;
my @bin;
 
for ^100000 {
my &s_of_n = s_of_n_creator( 3);
mysink @sample.&s_of_n for ^9;
for @itemsbin[$_]++ ->for $items_of_n {9;
@sample = s_of_n($item);
}
for @sample -> $s {
@bin[$s]++;
}
}
 
say @bin;</syntaxhighlight>
Output:
2,392

edits