How to resolve the algorithm One of n lines in a file step by step in the Run BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm One of n lines in a file step by step in the Run BASIC programming language

Table of Contents

Problem Statement

A method of choosing a line randomly from a file: Is to:

Note: You may choose a smaller number of repetitions if necessary, but mention this up-front. Note: This is a specific version of a Reservoir Sampling algorithm: https://en.wikipedia.org/wiki/Reservoir_sampling

Let's start with the solution:

Step by Step solution about How to resolve the algorithm One of n lines in a file step by step in the Run BASIC programming language

Source code in the run programming language

for i1 = 1 to 1000000
    c = oneOfN(10)
    chosen(c) = chosen(c) + 1
next
 
for i1 = 1 to 10
    print i1;" ";chosen(i1)
next
 
FUNCTION oneOfN(n)
    for i2 = 1 to n
        IF int(rnd(1) * i2) = 0 then choice = i2
    next
    oneOfN = choice
END FUNCTION

  

You may also check:How to resolve the algorithm Jensen's Device step by step in the Joy programming language
You may also check:How to resolve the algorithm Terminal control/Unicode output step by step in the Wren programming language
You may also check:How to resolve the algorithm Bitcoin/address validation step by step in the Racket programming language
You may also check:How to resolve the algorithm Pascal matrix generation step by step in the Stata programming language
You may also check:How to resolve the algorithm Knapsack problem/Unbounded step by step in the Forth programming language