Skip to content

Commit

Permalink
Upgrade rand to 0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
KAAtheWiseGit committed Feb 16, 2025
1 parent a3c2610 commit 2a50859
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 114 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ rkyv-serialize = ["rkyv-serialize-no-std", "rkyv/std", "rkyv/validation"]
## To use rand in a #[no-std] environment, enable the
## `rand-no-std` feature instead of `rand`.
rand-no-std = ["rand-package"]
rand = ["rand-no-std", "rand-package/std", "rand-package/std_rng", "rand_distr"]
rand = ["rand-no-std", "rand-package/std", "rand-package/std_rng", "rand-package/thread_rng", "rand_distr"]

# Tests
arbitrary = ["quickcheck"]
Expand All @@ -77,14 +77,14 @@ rkyv-safe-deser = ["rkyv-serialize", "rkyv/validation"]
[dependencies]
nalgebra-macros = { version = "0.2.2", path = "nalgebra-macros", optional = true }
typenum = "1.12"
rand-package = { package = "rand", version = "0.8", optional = true, default-features = false }
rand-package = { package = "rand", version = "0.9", optional = true, default-features = false }
num-traits = { version = "0.2", default-features = false }
num-complex = { version = "0.4", default-features = false }
num-rational = { version = "0.4", default-features = false }
approx = { version = "0.5", default-features = false }
simba = { version = "0.9", default-features = false }
alga = { version = "0.9", default-features = false, optional = true }
rand_distr = { version = "0.4", default-features = false, optional = true }
rand_distr = { version = "0.5", default-features = false, optional = true }
matrixmultiply = { version = "0.3", optional = true }
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
rkyv = { version = "0.7.41", default-features = false, optional = true }
Expand Down Expand Up @@ -114,7 +114,7 @@ rayon = { version = "1.6", optional = true }

[dev-dependencies]
serde_json = "1.0"
rand_xorshift = "0.3"
rand_xorshift = "0.4"
rand_isaac = "0.3"
criterion = { version = "0.4", features = ["html_reports"] }
nalgebra = { path = ".", features = ["debug", "compare", "rand", "macros"] }
Expand Down
24 changes: 13 additions & 11 deletions src/base/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use quickcheck::{Arbitrary, Gen};
use num::{Bounded, One, Zero};
#[cfg(feature = "rand-no-std")]
use rand::{
distributions::{Distribution, Standard},
distr::{Distribution, StandardUniform},
Rng,
};

Expand Down Expand Up @@ -293,10 +293,10 @@ where
#[cfg(feature = "rand")]
pub fn new_random_generic(nrows: R, ncols: C) -> Self
where
Standard: Distribution<T>,
StandardUniform: Distribution<T>,
{
let mut rng = rand::thread_rng();
Self::from_fn_generic(nrows, ncols, |_, _| rng.gen())
let mut rng = rand::rng();
Self::from_fn_generic(nrows, ncols, |_, _| rng.random())
}

/// Creates a matrix filled with random values from the given distribution.
Expand Down Expand Up @@ -637,7 +637,7 @@ macro_rules! impl_constructors(
#[inline]
#[cfg(feature = "rand")]
pub fn new_random($($args: usize),*) -> Self
where Standard: Distribution<T> {
where StandardUniform: Distribution<T> {
Self::new_random_generic($($gargs),*)
}
}
Expand Down Expand Up @@ -856,17 +856,19 @@ where
}

#[cfg(feature = "rand-no-std")]
impl<T: Scalar, R: Dim, C: Dim> Distribution<OMatrix<T, R, C>> for Standard
impl<T: Scalar, R: Dim, C: Dim> Distribution<OMatrix<T, R, C>> for StandardUniform
where
DefaultAllocator: Allocator<R, C>,
Standard: Distribution<T>,
StandardUniform: Distribution<T>,
{
#[inline]
fn sample<G: Rng + ?Sized>(&self, rng: &mut G) -> OMatrix<T, R, C> {
let nrows = R::try_to_usize().unwrap_or_else(|| rng.gen_range(0..10));
let ncols = C::try_to_usize().unwrap_or_else(|| rng.gen_range(0..10));
let nrows = R::try_to_usize().unwrap_or_else(|| rng.random_range(0..10));
let ncols = C::try_to_usize().unwrap_or_else(|| rng.random_range(0..10));

OMatrix::from_fn_generic(R::from_usize(nrows), C::from_usize(ncols), |_, _| rng.gen())
OMatrix::from_fn_generic(R::from_usize(nrows), C::from_usize(ncols), |_, _| {
rng.random()
})
}
}

Expand All @@ -892,7 +894,7 @@ where

// TODO(specialization): faster impls possible for D≤4 (see rand_distr::{UnitCircle, UnitSphere})
#[cfg(feature = "rand")]
impl<T: crate::RealField, D: DimName> Distribution<Unit<OVector<T, D>>> for Standard
impl<T: crate::RealField, D: DimName> Distribution<Unit<OVector<T, D>>> for StandardUniform
where
DefaultAllocator: Allocator<D>,
rand_distr::StandardNormal: Distribution<T>,
Expand Down
6 changes: 3 additions & 3 deletions src/base/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use quickcheck::{Arbitrary, Gen};

#[cfg(feature = "rand-no-std")]
use rand::{
distributions::{Distribution, Standard},
distr::{Distribution, StandardUniform},
Rng,
};

Expand All @@ -24,8 +24,8 @@ pub fn reject<F: FnMut(&T) -> bool, T: Arbitrary>(g: &mut Gen, f: F) -> T {
#[cfg(feature = "rand-no-std")]
pub fn reject_rand<G: Rng + ?Sized, F: FnMut(&T) -> bool, T>(g: &mut G, f: F) -> T
where
Standard: Distribution<T>,
StandardUniform: Distribution<T>,
{
use std::iter;
iter::repeat(()).map(|_| g.gen()).find(f).unwrap()
iter::repeat(()).map(|_| g.random()).find(f).unwrap()
}
8 changes: 4 additions & 4 deletions src/geometry/isometry_construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use quickcheck::{Arbitrary, Gen};
use num::One;
#[cfg(feature = "rand-no-std")]
use rand::{
distributions::{Distribution, Standard},
distr::{Distribution, StandardUniform},
Rng,
};

Expand Down Expand Up @@ -89,14 +89,14 @@ where
}

#[cfg(feature = "rand-no-std")]
impl<T: crate::RealField, R, const D: usize> Distribution<Isometry<T, R, D>> for Standard
impl<T: crate::RealField, R, const D: usize> Distribution<Isometry<T, R, D>> for StandardUniform
where
R: AbstractRotation<T, D>,
Standard: Distribution<T> + Distribution<R>,
StandardUniform: Distribution<T> + Distribution<R>,
{
#[inline]
fn sample<G: Rng + ?Sized>(&self, rng: &mut G) -> Isometry<T, R, D> {
Isometry::from_parts(rng.gen(), rng.gen())
Isometry::from_parts(rng.random(), rng.random())
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/geometry/orthographic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use quickcheck::{Arbitrary, Gen};
#[cfg(feature = "rand-no-std")]
use rand::{
distributions::{Distribution, Standard},
distr::{Distribution, StandardUniform},
Rng,
};
#[cfg(feature = "serde-serialize-no-std")]
Expand Down Expand Up @@ -741,18 +741,18 @@ impl<T: RealField> Orthographic3<T> {
}

#[cfg(feature = "rand-no-std")]
impl<T: RealField> Distribution<Orthographic3<T>> for Standard
impl<T: RealField> Distribution<Orthographic3<T>> for StandardUniform
where
Standard: Distribution<T>,
StandardUniform: Distribution<T>,
{
/// Generate an arbitrary random variate for testing purposes.
fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> Orthographic3<T> {
use crate::base::helper;
let left = r.gen();
let left = r.random();
let right = helper::reject_rand(r, |x: &T| *x > left);
let bottom = r.gen();
let bottom = r.random();
let top = helper::reject_rand(r, |x: &T| *x > bottom);
let znear = r.gen();
let znear = r.random();
let zfar = helper::reject_rand(r, |x: &T| *x > znear);

Orthographic3::new(left, right, bottom, top, znear, zfar)
Expand Down
10 changes: 5 additions & 5 deletions src/geometry/perspective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use quickcheck::{Arbitrary, Gen};
#[cfg(feature = "rand-no-std")]
use rand::{
distributions::{Distribution, Standard},
distr::{Distribution, StandardUniform},
Rng,
};

Expand Down Expand Up @@ -313,18 +313,18 @@ impl<T: RealField> Perspective3<T> {
}

#[cfg(feature = "rand-no-std")]
impl<T: RealField> Distribution<Perspective3<T>> for Standard
impl<T: RealField> Distribution<Perspective3<T>> for StandardUniform
where
Standard: Distribution<T>,
StandardUniform: Distribution<T>,
{
/// Generate an arbitrary random variate for testing purposes.
fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> Perspective3<T> {
use crate::base::helper;
let znear = r.gen();
let znear = r.random();
let zfar = helper::reject_rand(r, |x: &T| !(x.clone() - znear.clone()).is_zero());
let aspect = helper::reject_rand(r, |x: &T| !x.is_zero());

Perspective3::new(aspect, r.gen(), znear, zfar)
Perspective3::new(aspect, r.random(), znear, zfar)
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/geometry/point_construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use quickcheck::{Arbitrary, Gen};
use num::{Bounded, One, Zero};
#[cfg(feature = "rand-no-std")]
use rand::{
distributions::{Distribution, Standard},
distr::{Distribution, StandardUniform},
Rng,
};

Expand Down Expand Up @@ -159,15 +159,15 @@ where
}

#[cfg(feature = "rand-no-std")]
impl<T: Scalar, D: DimName> Distribution<OPoint<T, D>> for Standard
impl<T: Scalar, D: DimName> Distribution<OPoint<T, D>> for StandardUniform
where
Standard: Distribution<T>,
StandardUniform: Distribution<T>,
DefaultAllocator: Allocator<D>,
{
/// Generate a `Point` where each coordinate is an independent variate from `[0, 1)`.
#[inline]
fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> OPoint<T, D> {
OPoint::from(rng.gen::<OVector<T, D>>())
OPoint::from(rng.random::<OVector<T, D>>())
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/geometry/quaternion_construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use quickcheck::{Arbitrary, Gen};

#[cfg(feature = "rand-no-std")]
use rand::{
distributions::{uniform::SampleUniform, Distribution, OpenClosed01, Standard, Uniform},
distr::{uniform::SampleUniform, Distribution, OpenClosed01, StandardUniform, Uniform},
Rng,
};

Expand Down Expand Up @@ -171,13 +171,13 @@ where
}

#[cfg(feature = "rand-no-std")]
impl<T: SimdRealField> Distribution<Quaternion<T>> for Standard
impl<T: SimdRealField> Distribution<Quaternion<T>> for StandardUniform
where
Standard: Distribution<T>,
StandardUniform: Distribution<T>,
{
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Quaternion<T> {
Quaternion::new(rng.gen(), rng.gen(), rng.gen(), rng.gen())
Quaternion::new(rng.random(), rng.random(), rng.random(), rng.random())
}
}

Expand Down Expand Up @@ -866,7 +866,7 @@ where
}

#[cfg(feature = "rand-no-std")]
impl<T: SimdRealField> Distribution<UnitQuaternion<T>> for Standard
impl<T: SimdRealField> Distribution<UnitQuaternion<T>> for StandardUniform
where
T::Element: SimdRealField,
OpenClosed01: Distribution<T>,
Expand All @@ -879,7 +879,8 @@ where
// Uniform random rotations.
// In D. Kirk, editor, Graphics Gems III, pages 124-132. Academic, New York, 1992.
let x0 = rng.sample(OpenClosed01);
let twopi = Uniform::new(T::zero(), T::simd_two_pi());
let twopi = Uniform::new(T::zero(), T::simd_two_pi())
.expect("Failed to costruct `Uniform`, should be unreachable");
let theta1 = rng.sample(&twopi);
let theta2 = rng.sample(&twopi);
let s1 = theta1.clone().simd_sin();
Expand Down Expand Up @@ -921,7 +922,7 @@ mod tests {
fn random_unit_quats_are_unit() {
let mut rng = rand_xorshift::XorShiftRng::from_seed([0xAB; 16]);
for _ in 0..1000 {
let x = rng.gen::<UnitQuaternion<f32>>();
let x = rng.random::<UnitQuaternion<f32>>();
assert!(relative_eq!(x.into_inner().norm(), 1.0))
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/geometry/rotation_specialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use num::Zero;

#[cfg(feature = "rand-no-std")]
use rand::{
distributions::{uniform::SampleUniform, Distribution, OpenClosed01, Standard, Uniform},
distr::{uniform::SampleUniform, Distribution, OpenClosed01, StandardUniform, Uniform},
Rng,
};

Expand Down Expand Up @@ -271,15 +271,17 @@ impl<T: SimdRealField> Rotation2<T> {
}

#[cfg(feature = "rand-no-std")]
impl<T: SimdRealField> Distribution<Rotation2<T>> for Standard
impl<T: SimdRealField> Distribution<Rotation2<T>> for StandardUniform
where
T::Element: SimdRealField,
T: SampleUniform,
{
/// Generate a uniformly distributed random rotation.
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Rotation2<T> {
let twopi = Uniform::new(T::zero(), T::simd_two_pi());
let twopi = Uniform::new(T::zero(), T::simd_two_pi())
.expect("Failed to costruct `Uniform`, should be unreachable");

Rotation2::new(rng.sample(twopi))
}
}
Expand Down Expand Up @@ -1153,7 +1155,7 @@ impl<T: SimdRealField> Rotation3<T> {
}

#[cfg(feature = "rand-no-std")]
impl<T: SimdRealField> Distribution<Rotation3<T>> for Standard
impl<T: SimdRealField> Distribution<Rotation3<T>> for StandardUniform
where
T::Element: SimdRealField,
OpenClosed01: Distribution<T>,
Expand All @@ -1167,7 +1169,8 @@ where
// In D. Kirk, editor, Graphics Gems III, pages 117-120. Academic, New York, 1992.

// Compute a random rotation around Z
let twopi = Uniform::new(T::zero(), T::simd_two_pi());
let twopi = Uniform::new(T::zero(), T::simd_two_pi())
.expect("Failed to costruct `Uniform`, should be unreachable");
let theta = rng.sample(&twopi);
let (ts, tc) = theta.simd_sin_cos();
let a = SMatrix::<T, 3, 3>::new(
Expand Down
8 changes: 4 additions & 4 deletions src/geometry/scale_construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use quickcheck::{Arbitrary, Gen};
use num::One;
#[cfg(feature = "rand-no-std")]
use rand::{
distributions::{Distribution, Standard},
distr::{Distribution, StandardUniform},
Rng,
};

Expand Down Expand Up @@ -63,14 +63,14 @@ impl<T: Scalar + One + ClosedMulAssign, const D: usize> One for Scale<T, D> {
}

#[cfg(feature = "rand-no-std")]
impl<T: Scalar, const D: usize> Distribution<Scale<T, D>> for Standard
impl<T: Scalar, const D: usize> Distribution<Scale<T, D>> for StandardUniform
where
Standard: Distribution<T>,
StandardUniform: Distribution<T>,
{
/// Generate an arbitrary random variate for testing purposes.
#[inline]
fn sample<G: Rng + ?Sized>(&self, rng: &mut G) -> Scale<T, D> {
Scale::from(rng.gen::<SVector<T, D>>())
Scale::from(rng.random::<SVector<T, D>>())
}
}

Expand Down
Loading

0 comments on commit 2a50859

Please sign in to comment.