#random #rng #streaming

reservoir-sampling

Implementations of a variety of algorithms for reservoir sampling in Rust

10 unstable releases (3 breaking)

0.5.1 Apr 27, 2021
0.5.0 Apr 17, 2021
0.4.2 Apr 12, 2021
0.4.1 Mar 26, 2021
0.1.1 Dec 17, 2020

#335 in Algorithms

Download history 5/week @ 2022-03-08 212/week @ 2022-03-15 41/week @ 2022-03-22 14/week @ 2022-03-29 9/week @ 2022-04-05 6/week @ 2022-04-12 7/week @ 2022-04-19 81/week @ 2022-04-26 66/week @ 2022-05-03 162/week @ 2022-05-10 34/week @ 2022-05-17 73/week @ 2022-05-24 97/week @ 2022-05-31 24/week @ 2022-06-07 10/week @ 2022-06-14 14/week @ 2022-06-21

159 downloads per month
Used in sparse_linear_assignment

Unlicense

11KB
129 lines

Gitpod ready-to-code Crates.io

reservoir-sampling

Crate implementing reservoir sampling, a method for getting random samples from a source in a single pass. Useful in situations where size of source is unknown or very large. Read this article for more information: https://en.wikipedia.org/wiki/Reservoir_sampling

All algorithms implemented here have been taken from this article only.


(This crate supports WASM)

Quickstart

use reservoir_sampling::unweighted::l;

fn main () {
    let mut sampled_arr = vec![0usize; 10];

    l(0usize..100, sampled_arr.as_mut_slice());
    println!("Sampled array: {:?}", sampled_arr);
}

API Design

Functions take:

  • An Iterator over generic type T, with no constraints which serves as a stream of data to sample.
  • Mutable array slice (&mut [T]) to store sampled data

By default, functions use rand::thread_rng to provide RNG. To use your own RNG which implements rand::RNG, use functions in reservoir_sampling::core.

Future development:

Stabilize weighted and implement more algorithms.

Dependencies

~0–345KB