loaded_dice 0.2.2

A simple sampler for loaded dices, implementing the alias method
Documentation
  • Coverage
  • 0%
    0 out of 5 items documented0 out of 3 items with examples
  • Size
  • Source code size: 56.06 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 188.62 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • eqv/loaded_dice
    2 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • eqv

Loaded Dice

A simple crate that implements a random sampler implementing the alias method (https://en.wikipedia.org/wiki/Alias_method). It can be used to sample from discrete probability distributions efficiently (O(1) per sample). One uses it by passing a vector of probabilites to the constructor. The constructor builds a data structure in O(n*n*log(n)) (Note: It would be quite possible to implement this in O(n*log(n)), however for reasonable sized number of values this method is faster than using the more effictient data strcutres. If the construction is slow in your case, you might consider using min/max heaps insteat of resorting the array after each construction step). This data structure can then be used to to sample a numbers between 0 and n with the corresponding probabilites.

Assume we want to sample from the following distirbution: p(0)=0.5, p(1)=0.3, p(2)=0.1, p(3)=0.1:

extern crate loaded_dice;
extern crate rand;

use loaded_dice::LoadedDiceSampler;
use rand::{thread_rng, Rng};
fn main() {
    let mut s = LoadedDiceSampler::new(vec!(0.5, 0.3, 0.1, 0.1), thread_rng());
    let iter: usize = 100;
    for i in (0..iter) {
        println!("{}", s.sample());
    }
}