How to resolve the algorithm Knuth's algorithm S step by step in the PicoLisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Knuth's algorithm S step by step in the PicoLisp 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 PicoLisp programming language

Source code in the picolisp programming language

(de s_of_n_creator (@N)
   (curry (@N (I . 0) (Res)) (Item)
      (cond
         ((>= @N (inc 'I)) (push 'Res Item))
         ((>= @N (rand 1 I)) (set (nth Res (rand 1 @N)) Item)) )
      Res ) )

(let Freq (need 10 0)
   (do 100000
      (let S_of_n (s_of_n_creator 3)
         (for I (mapc S_of_n (0 1 2 3 4 5 6 7 8 9))
            (inc (nth Freq (inc I))) ) ) )
   Freq )

  

You may also check:How to resolve the algorithm Subleq step by step in the Scala programming language
You may also check:How to resolve the algorithm Runge-Kutta method step by step in the Wren programming language
You may also check:How to resolve the algorithm Handle a signal step by step in the Clojure programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the PHP programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the jq programming language