1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#![cfg_attr(test, feature(test))]
#[cfg(test)]
extern crate assert;
#[cfg(test)]
extern crate test;
extern crate num;
extern crate rand;
extern crate special;
pub use ::rand::Rng as Generator;
pub use ::rand::thread_rng as generator;
pub mod distributions;
pub trait Distribution {
type Item;
fn cdf(&self, x: Self::Item) -> f64;
fn inv_cdf(&self, p: f64) -> Self::Item;
fn sample<G: Generator>(&self, generator: &mut G) -> Self::Item;
}
pub struct Sampler<D, G>(pub D, pub G);
impl<'a, T, D, G> Iterator for Sampler<&'a D, &'a mut G> where D: Distribution<Item=T>, G: Generator {
type Item = T;
#[inline]
fn next(&mut self) -> Option<T> {
Some(self.0.sample(self.1))
}
}