Skip to main content

Module distribution

Module distribution 

Source
Expand description

Pluggable Distribution trait and built-in samplers. Trait-based distributions and pluggable user-defined samplers.

Distribution<T> is the universal handshake between a sampler and an RNG: anything that can map a Random into a T is a distribution. The built-in samplers (Normal, Exponential, Uniform, Poisson) live here as concrete impls and forward to the optimised methods on Random. Users add their own distributions by implementing the trait.

§Examples

use vrd::{Random, Distribution};
use vrd::distribution::{Normal, Exponential};

let mut rng = Random::from_u64_seed(1);
let z = Normal { mu: 0.0, sigma: 1.0 }.sample(&mut rng);
let x = Exponential { rate: 1.5 }.sample(&mut rng);
use vrd::{Random, Distribution};

// User-defined distribution: Bernoulli(p).
struct Bernoulli { p: f64 }

impl Distribution<bool> for Bernoulli {
    fn sample(&self, rng: &mut Random) -> bool {
        rng.double() < self.p
    }
}

let mut rng = Random::from_u64_seed(1);
let coin = Bernoulli { p: 0.5 }.sample(&mut rng);

Structs§

Exponential
Exponential with rate lambda. Mean is 1/lambda.
Iter
Iterator returned by Distribution::samples.
Normal
Standard normal N(mu, sigma^2) - Ziggurat sampler, see Random::normal.
Poisson
Poisson with mean lambda.
Uniform
Continuous uniform on [low, high).

Traits§

Distribution
A distribution that can be sampled with a mutable Random.