Skip to content

bediger4000/reservoir-sampling

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Reservoir Sampling Filter

This code makes a Unix-style filter program that chooses some number of lines from stdin at random, then prints those lines to stdout.

Building and Running

$ go build reservoir.go
$ ./reservoir 7 < /usr/share/dict/words
escutcheons
insinuator's
bone
cc
identification's
segues
comer
$

Input is from stdin, output on stdout. The mandatory command line argument is the number of lines to select from stdin.

This code based on Algorithm R from Wikipedia.

Daily Coding Problem: Problem #911 [Medium]

This problem was asked by Facebook.

Given a stream of elements too large to store in memory, pick a random element from the stream with uniform probability.

Analysis

Isn't this just reservoir sampling, with a reservoir size of 1?

About

Reservoir sampling as a Unix filter program

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages