#random #streaming #reservoir #sampling #algorithm #variety

reservoir-sampling

Rust中多种水库抽样算法的实现

10个不稳定版本 (3个破坏性更改)

0.5.1 2021年4月27日
0.5.0 2021年4月17日
0.4.2 2021年4月12日
0.4.1 2021年3月26日
0.1.1 2020年12月17日

#2207 in 算法

每月28次下载
用于 sparse_linear_assignment

无许可

12KB
129

Gitpod ready-to-code Crates.io

reservoir-sampling

实现了水库抽样,一种从源中单次获取随机样本的方法。在源大小未知或非常大时非常有用。阅读此文章以获取更多信息:https://en.wikipedia.org/wiki/Reservoir_sampling

此处实现的所有算法均来自该文章。


(此包支持WASM)

快速入门

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设计

函数接收

  • 通配类型 IteratorT 迭代器,没有约束,作为要抽样的数据流。
  • 用于存储抽样数据的可变数组切片(&mut [T]

默认情况下,函数使用 rand::thread_rng 提供RNG。要使用自己的RNG,该RNG实现了 rand::RNG,请使用 reservoir_sampling::core 中的函数。

未来开发

稳定 weighted 并实现更多算法。

依赖项

~0–455KB