How to resolve the algorithm One of n lines in a file step by step in the ALGOL 68 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 ALGOL 68 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 ALGOL 68 programming language
Source code in the algol programming language
BEGIN
INT max lines = 10; CO Should be read from a file. CO
[max lines]INT stats;
FOR i TO max lines DO stats[i] := 0 OD;
first random (42); CO Should have rather more entropy! CO
PROC one of n = (INT n) INT :
BEGIN
INT result := 1;
FOR i TO n DO (random < 1/i | result := i) OD;
result
END;
TO 1000000 DO stats[one of n (max lines)] +:= 1 OD;
print (("Line Number times chosen", newline));
FOR i TO max lines DO printf (($g(0)7xg(0)l$, i, stats[i])) OD
END
You may also check:How to resolve the algorithm Send an unknown method call step by step in the Tcl programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the Go programming language
You may also check:How to resolve the algorithm Damm algorithm step by step in the Groovy programming language
You may also check:How to resolve the algorithm File extension is in extensions list step by step in the Objeck programming language
You may also check:How to resolve the algorithm Bitmap/Midpoint circle algorithm step by step in the Perl programming language