#random #sampling #algorithm

aliasmethod

Implementation of Walker's Alias method

4 releases (breaking)

0.4.0 Aug 6, 2019
0.3.0 Jun 6, 2019
0.2.0 Jun 6, 2019
0.1.0 Aug 23, 2016

#58 in #sampling

Download history 3/week @ 2024-07-21 5/week @ 2024-07-28 3/week @ 2024-08-04 5/week @ 2024-08-11 1/week @ 2024-08-18 5/week @ 2024-09-01 6/week @ 2024-09-08 8/week @ 2024-09-15 21/week @ 2024-09-22 14/week @ 2024-09-29 8/week @ 2024-10-06 24/week @ 2024-10-13 14/week @ 2024-10-20 12/week @ 2024-11-03

51 downloads per month
Used in locustdb

Custom license

7KB
119 lines

aliasmethod

Build Status

Implementation of Walker's Alias method by Rust.

The algorithm is principally useful when you need to random sampling with replacement by O(1).

Example

use rand::XorShiftRng;
use aliasmethod::AliasTable

let weights = vec![1.0, 1.0, 8.0];

let alias_table = AliasTable::new(weights)?;

let rng = XorShiftRng::from_seed([189522394, 1694417663, 1363148323, 4087496301]);
let n = alias_table.random(rng);

assert!(0 <= n && n <= weights.length);

Dependencies

~1.4–2MB
~35K SLoC