How to resolve the algorithm Knuth's algorithm S step by step in the zkl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Knuth's algorithm S step by step in the zkl programming language
Table of Contents
Problem Statement
This is a method of randomly sampling n items from a set of M items, with equal probability; where M >= n and M, the number of items is unknown until the end. This means that the equal probability sampling should be maintained for all successive items > n as they become available (although the content of successive samples can change).
Note: A class taking n and generating a callable instance/function might also be used.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Knuth's algorithm S step by step in the zkl programming language
Source code in the zkl programming language
fcn s_of_n_creator(n){
fcn(item,ri,N,samples){
i:=ri.inc(); // 1,2,3,4,...
if(i<=N) samples.append(item);
else if ((0).random(i) < N) samples[(0).random(N)] = item;
samples
}.fp1(Ref(1),n,L())
}
s3:=s_of_n_creator(3);
[0..9].pump(List,s3,"copy").println();
dist:=L(0,0,0,0,0,0,0,0,0,0);
do(0d100_000){
(0).pump(10,Void,s_of_n_creator(3)).apply2('wrap(n){dist[n]=dist[n]+1})
}
N:=dist.sum();
dist.apply('wrap(n){"%.2f%%".fmt(n.toFloat()/N*100)}).println();
You may also check:How to resolve the algorithm Send an unknown method call step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Deal cards for FreeCell step by step in the Quackery programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Thiele's interpolation formula step by step in the Ada programming language
You may also check:How to resolve the algorithm Deal cards for FreeCell step by step in the Perl programming language