2025-01-08 12:58:40 +01:00
2025-01-08 12:56:53 +01:00
2025-01-08 12:56:53 +01:00
2025-01-08 12:58:40 +01:00
2024-11-22 15:44:54 +01:00
2025-01-08 12:56:53 +01:00
2024-11-28 17:43:25 +01:00

ranlin — Choose a random line from a text file

Implemented using an elegant algorithm, originally stolen from the "Perl Cookbook", which does not have more than a single text line in memory at any time.

The trivial way to pick a random line would of course be to read all lines into an array, and then pick a random index for that array.

The algorithm used here, however, will generate a random number between zero and the number of lines read, every time a line is read. Then, if that number is greater than one, the line will be taken as the resulting line, for that moment.

Of course, when more lines are read after that, any line can be picked in that fashion, where the chance the line is picked is still equal for evey line.

Brilliant.

Build

make

Install

sudo make install

or, on OpenBSD:

doas gmake install

Run

ranlin <textfile>

Output will be some random line from the file.

Description
Return a random line from a text file. Elegant algorithm which never remembers more than one line at a time.
Readme 47 KiB
Languages
Makefile 59.3%
C++ 40.7%