use Rng;
#[doc(inline)] pub use self::other::Alphanumeric;
#[doc(inline)] pub use self::uniform::Uniform;
#[doc(inline)] pub use self::float::{OpenClosed01, Open01};
#[deprecated(since="0.5.0", note="use Uniform instead")]
pub use self::uniform::Uniform as Range;
#[cfg(feature="std")]
#[doc(inline)] pub use self::gamma::{Gamma, ChiSquared, FisherF, StudentT};
#[cfg(feature="std")]
#[doc(inline)] pub use self::normal::{Normal, LogNormal, StandardNormal};
#[cfg(feature="std")]
#[doc(inline)] pub use self::exponential::{Exp, Exp1};
#[cfg(feature="std")]
#[doc(inline)] pub use self::pareto::Pareto;
#[cfg(feature = "std")]
#[doc(inline)] pub use self::poisson::Poisson;
#[cfg(feature = "std")]
#[doc(inline)] pub use self::binomial::Binomial;
#[doc(inline)] pub use self::bernoulli::Bernoulli;
#[cfg(feature = "std")]
#[doc(inline)] pub use self::cauchy::Cauchy;
pub mod uniform;
#[cfg(feature="std")]
#[doc(hidden)] pub mod gamma;
#[cfg(feature="std")]
#[doc(hidden)] pub mod normal;
#[cfg(feature="std")]
#[doc(hidden)] pub mod exponential;
#[cfg(feature="std")]
#[doc(hidden)] pub mod pareto;
#[cfg(feature = "std")]
#[doc(hidden)] pub mod poisson;
#[cfg(feature = "std")]
#[doc(hidden)] pub mod binomial;
#[doc(hidden)] pub mod bernoulli;
#[cfg(feature = "std")]
#[doc(hidden)] pub mod cauchy;
mod float;
mod integer;
#[cfg(feature="std")]
mod log_gamma;
mod other;
#[cfg(feature="std")]
mod ziggurat_tables;
#[cfg(feature="std")]
use distributions::float::IntoFloat;
#[deprecated(since="0.5.0", note="use Distribution instead")]
pub trait Sample<Support> {
fn sample<R: Rng>(&mut self, rng: &mut R) -> Support;
}
#[allow(deprecated)]
#[deprecated(since="0.5.0", note="use Distribution instead")]
pub trait IndependentSample<Support>: Sample<Support> {
fn ind_sample<R: Rng>(&self, &mut R) -> Support;
}
#[deprecated(since="0.5.0", note="use uniform instead")]
pub mod range {
pub use distributions::uniform::Uniform as Range;
pub use distributions::uniform::SampleUniform as SampleRange;
}
#[allow(deprecated)]
mod impls {
use Rng;
use distributions::{Distribution, Sample, IndependentSample,
WeightedChoice};
#[cfg(feature="std")]
use distributions::exponential::Exp;
#[cfg(feature="std")]
use distributions::gamma::{Gamma, ChiSquared, FisherF, StudentT};
#[cfg(feature="std")]
use distributions::normal::{Normal, LogNormal};
use distributions::range::{Range, SampleRange};
impl<'a, T: Clone> Sample<T> for WeightedChoice<'a, T> {
fn sample<R: Rng>(&mut self, rng: &mut R) -> T {
Distribution::sample(self, rng)
}
}
impl<'a, T: Clone> IndependentSample<T> for WeightedChoice<'a, T> {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> T {
Distribution::sample(self, rng)
}
}
impl<T: SampleRange> Sample<T> for Range<T> {
fn sample<R: Rng>(&mut self, rng: &mut R) -> T {
Distribution::sample(self, rng)
}
}
impl<T: SampleRange> IndependentSample<T> for Range<T> {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> T {
Distribution::sample(self, rng)
}
}
#[cfg(feature="std")]
macro_rules! impl_f64 {
($($name: ident), *) => {
$(
impl Sample<f64> for $name {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
Distribution::sample(self, rng)
}
}
impl IndependentSample<f64> for $name {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
Distribution::sample(self, rng)
}
}
)*
}
}
#[cfg(feature="std")]
impl_f64!(Exp, Gamma, ChiSquared, FisherF, StudentT, Normal, LogNormal);
}
pub trait Distribution<T> {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> T;
fn sample_iter<'a, R>(&'a self, rng: &'a mut R) -> DistIter<'a, Self, R, T>
where Self: Sized, R: Rng
{
DistIter {
distr: self,
rng: rng,
phantom: ::core::marker::PhantomData,
}
}
}
impl<'a, T, D: Distribution<T>> Distribution<T> for &'a D {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> T {
(*self).sample(rng)
}
}
#[derive(Debug)]
pub struct DistIter<'a, D: 'a, R: 'a, T> {
distr: &'a D,
rng: &'a mut R,
phantom: ::core::marker::PhantomData<T>,
}
impl<'a, D, R, T> Iterator for DistIter<'a, D, R, T>
where D: Distribution<T>, R: Rng + 'a
{
type Item = T;
#[inline(always)]
fn next(&mut self) -> Option<T> {
Some(self.distr.sample(self.rng))
}
fn size_hint(&self) -> (usize, Option<usize>) {
(usize::max_value(), None)
}
}
#[derive(Clone, Copy, Debug)]
pub struct Standard;
#[allow(deprecated)]
impl<T> ::Rand for T where Standard: Distribution<T> {
fn rand<R: Rng>(rng: &mut R) -> Self {
Standard.sample(rng)
}
}
#[derive(Copy, Clone, Debug)]
pub struct Weighted<T> {
pub weight: u32,
pub item: T,
}
#[derive(Debug)]
pub struct WeightedChoice<'a, T:'a> {
items: &'a mut [Weighted<T>],
weight_range: Uniform<u32>,
}
impl<'a, T: Clone> WeightedChoice<'a, T> {
pub fn new(items: &'a mut [Weighted<T>]) -> WeightedChoice<'a, T> {
assert!(!items.is_empty(), "WeightedChoice::new called with no items");
let mut running_total: u32 = 0;
for item in items.iter_mut() {
running_total = match running_total.checked_add(item.weight) {
Some(n) => n,
None => panic!("WeightedChoice::new called with a total weight \
larger than a u32 can contain")
};
item.weight = running_total;
}
assert!(running_total != 0, "WeightedChoice::new called with a total weight of 0");
WeightedChoice {
items,
weight_range: Uniform::new(0, running_total)
}
}
}
impl<'a, T: Clone> Distribution<T> for WeightedChoice<'a, T> {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> T {
let sample_weight = self.weight_range.sample(rng);
if sample_weight < self.items[0].weight {
return self.items[0].item.clone();
}
let mut idx = 0;
let mut modifier = self.items.len();
while modifier > 1 {
let i = idx + modifier / 2;
if self.items[i].weight <= sample_weight {
idx = i;
modifier += 1;
} else {
}
modifier /= 2;
}
self.items[idx + 1].item.clone()
}
}
#[cfg(feature="std")]
#[inline(always)]
fn ziggurat<R: Rng + ?Sized, P, Z>(
rng: &mut R,
symmetric: bool,
x_tab: ziggurat_tables::ZigTable,
f_tab: ziggurat_tables::ZigTable,
mut pdf: P,
mut zero_case: Z)
-> f64 where P: FnMut(f64) -> f64, Z: FnMut(&mut R, f64) -> f64 {
loop {
let bits = rng.next_u64();
let i = bits as usize & 0xff;
let u = if symmetric {
(bits >> 12).into_float_with_exponent(1) - 3.0
} else {
(bits >> 12).into_float_with_exponent(0)
- (1.0 - ::core::f64::EPSILON / 2.0)
};
let x = u * x_tab[i];
let test_x = if symmetric { x.abs() } else {x};
if test_x < x_tab[i + 1] {
return x;
}
if i == 0 {
return zero_case(rng, u);
}
if f_tab[i + 1] + (f_tab[i] - f_tab[i + 1]) * rng.gen::<f64>() < pdf(x) {
return x;
}
}
}
#[cfg(test)]
mod tests {
use Rng;
use rngs::mock::StepRng;
use super::{WeightedChoice, Weighted, Distribution};
#[test]
fn test_weighted_choice() {
macro_rules! t {
($items:expr, $expected:expr) => {{
let mut items = $items;
let mut total_weight = 0;
for item in &items { total_weight += item.weight; }
let wc = WeightedChoice::new(&mut items);
let expected = $expected;
let mut rng = StepRng::new(0, !0 / (total_weight as u64));
for &val in expected.iter() {
assert_eq!(wc.sample(&mut rng), val)
}
}}
}
t!([Weighted { weight: 1, item: 10}], [10]);
t!([Weighted { weight: 0, item: 20},
Weighted { weight: 2, item: 21},
Weighted { weight: 0, item: 22},
Weighted { weight: 1, item: 23}],
[21, 21, 23]);
t!([Weighted { weight: 4, item: 30},
Weighted { weight: 3, item: 31}],
[30, 31, 30, 31, 30, 31, 30]);
t!([Weighted { weight: 1, item: 40},
Weighted { weight: 1, item: 41},
Weighted { weight: 1, item: 42},
Weighted { weight: 1, item: 43},
Weighted { weight: 1, item: 44}],
[40, 41, 42, 43, 44]);
t!([Weighted { weight: 1, item: 50},
Weighted { weight: 1, item: 51},
Weighted { weight: 1, item: 52},
Weighted { weight: 1, item: 53},
Weighted { weight: 1, item: 54},
Weighted { weight: 1, item: 55},
Weighted { weight: 1, item: 56}],
[50, 54, 51, 55, 52, 56, 53]);
}
#[test]
fn test_weighted_clone_initialization() {
let initial : Weighted<u32> = Weighted {weight: 1, item: 1};
let clone = initial.clone();
assert_eq!(initial.weight, clone.weight);
assert_eq!(initial.item, clone.item);
}
#[test] #[should_panic]
fn test_weighted_clone_change_weight() {
let initial : Weighted<u32> = Weighted {weight: 1, item: 1};
let mut clone = initial.clone();
clone.weight = 5;
assert_eq!(initial.weight, clone.weight);
}
#[test] #[should_panic]
fn test_weighted_clone_change_item() {
let initial : Weighted<u32> = Weighted {weight: 1, item: 1};
let mut clone = initial.clone();
clone.item = 5;
assert_eq!(initial.item, clone.item);
}
#[test] #[should_panic]
fn test_weighted_choice_no_items() {
WeightedChoice::<isize>::new(&mut []);
}
#[test] #[should_panic]
fn test_weighted_choice_zero_weight() {
WeightedChoice::new(&mut [Weighted { weight: 0, item: 0},
Weighted { weight: 0, item: 1}]);
}
#[test] #[should_panic]
fn test_weighted_choice_weight_overflows() {
let x = ::core::u32::MAX / 2; WeightedChoice::new(&mut [Weighted { weight: x, item: 0 },
Weighted { weight: 1, item: 1 },
Weighted { weight: x, item: 2 },
Weighted { weight: 1, item: 3 }]);
}
#[test] #[allow(deprecated)]
fn test_backwards_compat_sample() {
use distributions::{Sample, IndependentSample};
struct Constant<T> { val: T }
impl<T: Copy> Sample<T> for Constant<T> {
fn sample<R: Rng>(&mut self, _: &mut R) -> T { self.val }
}
impl<T: Copy> IndependentSample<T> for Constant<T> {
fn ind_sample<R: Rng>(&self, _: &mut R) -> T { self.val }
}
let mut sampler = Constant{ val: 293 };
assert_eq!(sampler.sample(&mut ::test::rng(233)), 293);
assert_eq!(sampler.ind_sample(&mut ::test::rng(234)), 293);
}
#[cfg(feature="std")]
#[test] #[allow(deprecated)]
fn test_backwards_compat_exp() {
use distributions::{IndependentSample, Exp};
let sampler = Exp::new(1.0);
sampler.ind_sample(&mut ::test::rng(235));
}
#[cfg(feature="std")]
#[test]
fn test_distributions_iter() {
use distributions::Normal;
let mut rng = ::test::rng(210);
let distr = Normal::new(10.0, 10.0);
let results: Vec<_> = distr.sample_iter(&mut rng).take(100).collect();
println!("{:?}", results);
}
}