Make use of rand more idiomatic

This should improve performance and accuracy.
This commit is contained in:
Vinzent Steinberg 2021-04-10 03:13:46 -03:00
parent 5aa6033a3b
commit fd3a752409
4 changed files with 19 additions and 14 deletions

View File

@ -284,7 +284,8 @@ where
where where
Standard: Distribution<N>, Standard: Distribution<N>,
{ {
Self::from_fn_generic(nrows, ncols, |_, _| rand::random()) let mut rng = rand::thread_rng();
Self::from_fn_generic(nrows, ncols, |_, _| rng.gen())
} }
/// Creates a matrix filled with random values from the given distribution. /// Creates a matrix filled with random values from the given distribution.

View File

@ -7,7 +7,7 @@ use quickcheck::{Arbitrary, Gen};
#[cfg(feature = "rand-no-std")] #[cfg(feature = "rand-no-std")]
use rand::{ use rand::{
distributions::{Distribution, OpenClosed01, Standard}, distributions::{Distribution, OpenClosed01, Standard, Uniform, uniform::SampleUniform},
Rng, Rng,
}; };
@ -855,6 +855,7 @@ impl<N: SimdRealField> Distribution<UnitQuaternion<N>> for Standard
where where
N::Element: SimdRealField, N::Element: SimdRealField,
OpenClosed01: Distribution<N>, OpenClosed01: Distribution<N>,
N: SampleUniform,
{ {
/// Generate a uniformly distributed random rotation quaternion. /// Generate a uniformly distributed random rotation quaternion.
#[inline] #[inline]
@ -863,10 +864,9 @@ where
// Uniform random rotations. // Uniform random rotations.
// In D. Kirk, editor, Graphics Gems III, pages 124-132. Academic, New York, 1992. // In D. Kirk, editor, Graphics Gems III, pages 124-132. Academic, New York, 1992.
let x0 = rng.sample(OpenClosed01); let x0 = rng.sample(OpenClosed01);
let x1 = rng.sample(OpenClosed01); let twopi = Uniform::new(N::zero(), N::simd_two_pi());
let x2 = rng.sample(OpenClosed01); let theta1 = rng.sample(&twopi);
let theta1 = N::simd_two_pi() * x1; let theta2 = rng.sample(&twopi);
let theta2 = N::simd_two_pi() * x2;
let s1 = theta1.simd_sin(); let s1 = theta1.simd_sin();
let c1 = theta1.simd_cos(); let c1 = theta1.simd_cos();
let s2 = theta2.simd_sin(); let s2 = theta2.simd_sin();

View File

@ -7,7 +7,7 @@ use num::Zero;
#[cfg(feature = "rand-no-std")] #[cfg(feature = "rand-no-std")]
use rand::{ use rand::{
distributions::{Distribution, OpenClosed01, Standard}, distributions::{Distribution, OpenClosed01, Standard, Uniform, uniform::SampleUniform},
Rng, Rng,
}; };
@ -265,12 +265,13 @@ impl<N: SimdRealField> Rotation2<N> {
impl<N: SimdRealField> Distribution<Rotation2<N>> for Standard impl<N: SimdRealField> Distribution<Rotation2<N>> for Standard
where where
N::Element: SimdRealField, N::Element: SimdRealField,
OpenClosed01: Distribution<N>, N: SampleUniform,
{ {
/// Generate a uniformly distributed random rotation. /// Generate a uniformly distributed random rotation.
#[inline] #[inline]
fn sample<'a, R: Rng + ?Sized>(&self, rng: &'a mut R) -> Rotation2<N> { fn sample<'a, R: Rng + ?Sized>(&self, rng: &'a mut R) -> Rotation2<N> {
Rotation2::new(rng.sample(OpenClosed01) * N::simd_two_pi()) let twopi = Uniform::new(N::zero(), N::simd_two_pi());
Rotation2::new(rng.sample(twopi))
} }
} }
@ -923,6 +924,7 @@ impl<N: SimdRealField> Distribution<Rotation3<N>> for Standard
where where
N::Element: SimdRealField, N::Element: SimdRealField,
OpenClosed01: Distribution<N>, OpenClosed01: Distribution<N>,
N: SampleUniform,
{ {
/// Generate a uniformly distributed random rotation. /// Generate a uniformly distributed random rotation.
#[inline] #[inline]
@ -932,7 +934,8 @@ where
// In D. Kirk, editor, Graphics Gems III, pages 117-120. Academic, New York, 1992. // In D. Kirk, editor, Graphics Gems III, pages 117-120. Academic, New York, 1992.
// Compute a random rotation around Z // Compute a random rotation around Z
let theta = N::simd_two_pi() * rng.sample(OpenClosed01); let twopi = Uniform::new(N::zero(), N::simd_two_pi());
let theta = rng.sample(&twopi);
let (ts, tc) = theta.simd_sin_cos(); let (ts, tc) = theta.simd_sin_cos();
let a = MatrixN::<N, U3>::new( let a = MatrixN::<N, U3>::new(
tc, tc,
@ -947,7 +950,7 @@ where
); );
// Compute a random rotation *of* Z // Compute a random rotation *of* Z
let phi = N::simd_two_pi() * rng.sample(OpenClosed01); let phi = rng.sample(&twopi);
let z = rng.sample(OpenClosed01); let z = rng.sample(OpenClosed01);
let (ps, pc) = phi.simd_sin_cos(); let (ps, pc) = phi.simd_sin_cos();
let sqrt_z = z.simd_sqrt(); let sqrt_z = z.simd_sqrt();

View File

@ -3,7 +3,7 @@ use quickcheck::{Arbitrary, Gen};
#[cfg(feature = "rand-no-std")] #[cfg(feature = "rand-no-std")]
use rand::{ use rand::{
distributions::{Distribution, OpenClosed01, Standard}, distributions::{Distribution, Uniform, uniform::SampleUniform, Standard},
Rng, Rng,
}; };
@ -401,12 +401,13 @@ where
impl<N: SimdRealField> Distribution<UnitComplex<N>> for Standard impl<N: SimdRealField> Distribution<UnitComplex<N>> for Standard
where where
N::Element: SimdRealField, N::Element: SimdRealField,
OpenClosed01: Distribution<N>, N: SampleUniform,
{ {
/// Generate a uniformly distributed random `UnitComplex`. /// Generate a uniformly distributed random `UnitComplex`.
#[inline] #[inline]
fn sample<'a, R: Rng + ?Sized>(&self, rng: &mut R) -> UnitComplex<N> { fn sample<'a, R: Rng + ?Sized>(&self, rng: &mut R) -> UnitComplex<N> {
UnitComplex::from_angle(rng.sample(OpenClosed01) * N::simd_two_pi()) let twopi = Uniform::new(N::zero(), N::simd_two_pi());
UnitComplex::from_angle(rng.sample(twopi))
} }
} }