How to resolve the algorithm One of n lines in a file step by step in the FreeBASIC 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 FreeBASIC 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 FreeBASIC programming language
Source code in the freebasic programming language
Declare Function one_of_n (n As Long) As Long
Dim As Long L0, c, elegido(1 To 10)
Function one_of_n (n As Long) As Long
'asume que la primera línea es 1
Dim As Long L1, opcion
For L1 = 1 To n
If Int(Rnd * L1) = 0 Then opcion = L1
Next L1
one_of_n = opcion
End Function
Randomize Timer
For L0 = 1 To 1000000
c = one_of_n(10)
elegido(c) += 1
Next L0
For L0 = 1 To 10
Print Using "##. #######"; L0; elegido(L0)
Next L0
Sleep
You may also check:How to resolve the algorithm Pascal's triangle step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Sierpinski triangle/Graphical step by step in the Haskell programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the REXX programming language
You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the Action! programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the C programming language