Skip to content
/ randline Public
forked from alexwlchan/randline

Get a random selection of lines in a file using reservoir sampling

License

Notifications You must be signed in to change notification settings

edsu/randline

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

randline

This tool picks one or more random lines from a file (or anything else you pass to it).

$ randline < /usr/share/dict/words
Urania

$ randline 3 < /usr/share/dict/words
foolhardiness
rhinoscopic
wormhood

How it works

This tool uses reservoir sampling to select the random lines; in particular Algorithm L.

I wrote it as a way to understand how reservoir sampling works, and to try using Rust generics. Although the final tool only deals with strings, the underlying reservoir_sample can sample iterators of any type.

Installation

You can download compiled binaries from the GitHub releases.

Alternatively, you can install from source. You need Rust installed; I recommend using Rustup. Then clone this repository and compile the code:

$ git clone "https://github.com/alexwlchan/emptydir.git"
$ cd emptydir
$ cargo install --path .

Usage

You need to pipe input to randline. If you don't pass an argument, it will print a single random line.

$ randline < /usr/share/dict/words
blithen

You can choose the number of random lines to print by passing a single argument k:

$ randline 3 < /usr/share/dict/words
unprofessed
ragout
Tarpeia

You can also pipe the output of another command to it, for example if I wanted to find 5 random words starting with 'a':

$ grep '^a' /usr/share/dict/words | randline 5
approachabl
autecological
alogical
ambrain
anticonstitutionally

License

MIT.

About

Get a random selection of lines in a file using reservoir sampling

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%