Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ default = ["std", "nalgebra", "rand"]
std = ["nalgebra?/std", "rand?/std"]
# at the moment, all nalgebra features needs std
nalgebra = ["dep:nalgebra", "std"]
rand = ["dep:rand", "nalgebra?/rand"]
rand = ["dep:rand", "nalgebra?/rand", "rand?/std_rng"]

[dependencies]
approx = "0.5.0"
num-traits = "0.2.14"

[dependencies.rand]
version = "0.9.0"
version = "0.10.0"
optional = true
default-features = false

Expand Down
4 changes: 2 additions & 2 deletions src/distribution/bernoulli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ impl core::fmt::Display for Bernoulli {
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distr::Distribution<bool> for Bernoulli {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> bool {
rng.random_bool(self.p())
::rand::RngExt::random_bool(rng, self.p())
}
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distr::Distribution<f64> for Bernoulli {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
rng.sample::<bool, _>(self) as u8 as f64
::rand::RngExt::sample::<bool, _>(rng, self) as u8 as f64
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/distribution/binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl core::fmt::Display for Binomial {
impl ::rand::distr::Distribution<u64> for Binomial {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> u64 {
(0..self.n).fold(0, |acc, _| {
let n: f64 = rng.random();
let n: f64 = ::rand::RngExt::random(rng);
if n < self.p { acc + 1 } else { acc }
})
}
Expand All @@ -126,7 +126,7 @@ impl ::rand::distr::Distribution<u64> for Binomial {
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distr::Distribution<f64> for Binomial {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
rng.sample::<u64, _>(self) as f64
::rand::RngExt::sample::<u64, _>(rng, self) as f64
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/distribution/categorical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl Discrete<u64, f64> for Categorical {
#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
pub fn sample_unchecked<R: ::rand::Rng + ?Sized>(rng: &mut R, cdf: &[f64]) -> usize {
let draw = rng.random::<f64>() * cdf.last().unwrap();
let draw = ::rand::RngExt::random::<f64>(rng) * cdf.last().unwrap();
cdf.iter().position(|val| *val >= draw).unwrap()
}

Expand Down
3 changes: 2 additions & 1 deletion src/distribution/cauchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ impl core::fmt::Display for Cauchy {
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distr::Distribution<f64> for Cauchy {
fn sample<R: ::rand::Rng + ?Sized>(&self, r: &mut R) -> f64 {
self.location + self.scale * (f64::consts::PI * (r.random::<f64>() - 0.5)).tan()
let x = ::rand::RngExt::random::<f64>(r);
self.location + self.scale * (f64::consts::PI * (x - 0.5)).tan()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/distribution/discrete_uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ impl core::fmt::Display for DiscreteUniform {
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distr::Distribution<i64> for DiscreteUniform {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> i64 {
rng.random_range(self.min..=self.max)
::rand::RngExt::random_range(rng, self.min..=self.max)
}
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distr::Distribution<f64> for DiscreteUniform {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
rng.sample::<i64, _>(self) as f64
::rand::RngExt::sample::<i64, _>(rng, self) as f64
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/distribution/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ pub fn sample_unchecked<R: ::rand::Rng + ?Sized>(rng: &mut R, shape: f64, rate:
let mut afix = 1.0;
if shape < 1.0 {
a = shape + 1.0;
afix = rng.random::<f64>().powf(1.0 / shape);
afix = ::rand::RngExt::random::<f64>(rng).powf(1.0 / shape);
}

let d = a - 1.0 / 3.0;
Expand All @@ -428,7 +428,7 @@ pub fn sample_unchecked<R: ::rand::Rng + ?Sized>(rng: &mut R, shape: f64, rate:

v = v * v * v;
x = x * x;
let u: f64 = rng.random();
let u: f64 = ::rand::RngExt::random(rng);
if u < 1.0 - 0.0331 * x * x || u.ln() < 0.5 * x + d * (1.0 - v + v.ln()) {
return afix * d * v / rate;
}
Expand Down
3 changes: 3 additions & 0 deletions src/distribution/geometric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::prec;
use crate::statistics::*;
use core::f64;

#[cfg(feature = "rand")]
use rand::RngExt;

/// Implements the
/// [Geometric](https://en.wikipedia.org/wiki/Geometric_distribution)
/// distribution
Expand Down
3 changes: 2 additions & 1 deletion src/distribution/gumbel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ impl core::fmt::Display for Gumbel {
#[cfg(feature = "rand")]
impl ::rand::distr::Distribution<f64> for Gumbel {
fn sample<R: rand::Rng + ?Sized>(&self, r: &mut R) -> f64 {
self.location - self.scale * ((-(r.random::<f64>())).ln()).ln()
let x = ::rand::RngExt::random::<f64>(r);
self.location - self.scale * ((-x).ln()).ln()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/distribution/hypergeometric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl ::rand::distr::Distribution<u64> for Hypergeometric {
let mut x = 0;
loop {
let p = successes / population;
let next: f64 = rng.random();
let next: f64 = ::rand::RngExt::random(rng);
if next < p {
x += 1;
successes -= 1.0;
Expand All @@ -187,7 +187,7 @@ impl ::rand::distr::Distribution<u64> for Hypergeometric {
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distr::Distribution<f64> for Hypergeometric {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
rng.sample::<u64, _>(self) as f64
::rand::RngExt::sample::<u64, _>(rng, self) as f64
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/distribution/laplace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl core::fmt::Display for Laplace {
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distr::Distribution<f64> for Laplace {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
let x: f64 = rng.random_range(-0.5..0.5);
let x: f64 = ::rand::RngExt::random_range(rng, -0.5..0.5);
self.location - self.scale * x.signum() * (1. - 2. * x.abs()).ln()
}
}
Expand Down Expand Up @@ -550,9 +550,12 @@ mod tests {
#[test]
fn test_sample() {
use ::rand::distr::Distribution;
use ::rand::rngs::StdRng;
use ::rand::SeedableRng;

let l = create_ok(0.1, 0.5);
l.sample(&mut ::rand::rng());
let mut rng = StdRng::seed_from_u64(42);
l.sample(&mut rng);
}

#[cfg(feature = "rand")]
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/levy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl ::rand::distr::Distribution<f64> for Levy {
use rand::distr::OpenClosed01;

// Inverse transform sampling
let u: f64 = rng.sample(OpenClosed01);
let u: f64 = ::rand::RngExt::sample(rng, OpenClosed01);
self.mu + (0.5 * self.c) / erfc_inv(u).powf(2.0)
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/distribution/multivariate_normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ where
/// `μ` is the mean vector
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> OVector<f64, D> {
let d = crate::distribution::Normal::new(0., 1.).unwrap();
let z = OVector::from_distribution_generic(self.mu.shape_generic().0, Const::<1>, &d, rng);
let z =
OVector::from_fn_generic(self.mu.shape_generic().0, Const::<1>, |_, _| d.sample(rng));
(&self.cov_chol_decomp * z) + &self.mu
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/multivariate_students_t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ where
let s = ChiSquared::new(self.freedom).unwrap();
let w = (self.freedom / s.sample(rng)).sqrt();
let (r, c) = self.location.shape_generic();
let z = OVector::<f64, D>::from_distribution_generic(r, c, &d, rng);
let z = OVector::<f64, D>::from_fn_generic(r, c, |_, _| d.sample(rng));
(w * &self.scale_chol_decomp * z) + &self.location
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/negative_binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ mod tests {
#[cfg(all(feature = "rand", feature = "std"))]
fn test_sample() {
use crate::prec;
use rand::{distr::Distribution, SeedableRng, rngs::StdRng};
use rand::{distr::Distribution, rngs::StdRng, SeedableRng};

let dist = NegativeBinomial::new(4.0, 0.5).unwrap();
let mut rng = StdRng::seed_from_u64(1600);
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/pareto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl ::rand::distr::Distribution<f64> for Pareto {
use rand::distr::OpenClosed01;

// Inverse transform sampling
let u: f64 = rng.sample(OpenClosed01);
let u: f64 = ::rand::RngExt::sample(rng, OpenClosed01);
self.scale * u.powf(-1.0 / self.shape)
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/distribution/poisson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ impl Discrete<u64, f64> for Poisson {
#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
pub fn sample_unchecked<R: ::rand::Rng + ?Sized>(rng: &mut R, lambda: f64) -> f64 {
use rand::RngExt;

if lambda < 30.0 {
let limit = (-lambda).exp();
let mut count = 0.0;
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/triangular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ impl Continuous<f64, f64> for Triangular {
#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
fn sample_unchecked<R: ::rand::Rng + ?Sized>(rng: &mut R, min: f64, max: f64, mode: f64) -> f64 {
let f: f64 = rng.random();
let f: f64 = ::rand::RngExt::random(rng);
if f < (mode - min) / (max - min) {
min + (f * (max - min) * (mode - min)).sqrt()
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl core::fmt::Display for Uniform {
impl ::rand::distr::Distribution<f64> for Uniform {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
let d = rand::distr::Uniform::new_inclusive(self.min, self.max).unwrap();
rng.sample(d)
::rand::RngExt::sample(rng, d)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/distribution/weibull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl core::fmt::Display for Weibull {
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distr::Distribution<f64> for Weibull {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
let x: f64 = rng.random();
let x: f64 = ::rand::RngExt::random(rng);
self.scale * (-x.ln()).powf(1.0 / self.shape)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/ziggurat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::ziggurat_tables;
use rand::Rng;
use rand::distr::Open01;
use rand::{Rng, RngExt};

pub fn sample_std_normal<R: Rng + ?Sized>(rng: &mut R) -> f64 {
#[inline]
Expand Down