Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More idiomatic use of Rand #864

Merged
merged 4 commits into from
Apr 11, 2021
Merged
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: 3 additions & 1 deletion src/base/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ where
where
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.
Expand Down Expand Up @@ -852,6 +853,7 @@ where
}
}

// TODO(specialization): faster impls possible for D≤4 (see rand_distr::{UnitCircle, UnitSphere})
#[cfg(feature = "rand")]
impl<N: crate::RealField, D: DimName> Distribution<Unit<VectorN<N, D>>> for Standard
where
Expand Down
1 change: 1 addition & 0 deletions src/geometry/orthographic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ impl<N: RealField> Distribution<Orthographic3<N>> for Standard
where
Standard: Distribution<N>,
{
/// Generate an arbitrary random variate for testing purposes.
fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> Orthographic3<N> {
use crate::base::helper;
let left = r.gen();
Expand Down
1 change: 1 addition & 0 deletions src/geometry/perspective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ impl<N: RealField> Distribution<Perspective3<N>> for Standard
where
Standard: Distribution<N>,
{
/// Generate an arbitrary random variate for testing purposes.
fn sample<'a, R: Rng + ?Sized>(&self, r: &'a mut R) -> Perspective3<N> {
use crate::base::helper;
let znear = r.gen();
Expand Down
1 change: 1 addition & 0 deletions src/geometry/point_construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ where
DefaultAllocator: Allocator<N, D>,
Standard: Distribution<N>,
{
/// Generate a `Point` where each coordinate is an independent variate from `[0, 1)`.
#[inline]
fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> Point<N, D> {
Point::from(rng.gen::<VectorN<N, D>>())
Expand Down
10 changes: 5 additions & 5 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::{Distribution, OpenClosed01, Standard},
distributions::{Distribution, OpenClosed01, Standard, Uniform, uniform::SampleUniform},
Rng,
};

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

Expand Down Expand Up @@ -265,12 +265,13 @@ impl<N: SimdRealField> Rotation2<N> {
impl<N: SimdRealField> Distribution<Rotation2<N>> for Standard
where
N::Element: SimdRealField,
OpenClosed01: Distribution<N>,
N: SampleUniform,
{
/// Generate a uniformly distributed random rotation.
#[inline]
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))
}
}

Expand Down Expand Up @@ -923,6 +924,7 @@ impl<N: SimdRealField> Distribution<Rotation3<N>> for Standard
where
N::Element: SimdRealField,
OpenClosed01: Distribution<N>,
N: SampleUniform,
{
/// Generate a uniformly distributed random rotation.
#[inline]
Expand All @@ -932,7 +934,8 @@ where
// In D. Kirk, editor, Graphics Gems III, pages 117-120. Academic, New York, 1992.

// 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 a = MatrixN::<N, U3>::new(
tc,
Expand All @@ -947,7 +950,7 @@ where
);

// 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 (ps, pc) = phi.simd_sin_cos();
let sqrt_z = z.simd_sqrt();
Expand Down
1 change: 1 addition & 0 deletions src/geometry/similarity_construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ where
DefaultAllocator: Allocator<N, D>,
Standard: Distribution<N> + Distribution<R>,
{
/// Generate an arbitrary random variate for testing purposes.
#[inline]
fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> Similarity<N, D, R> {
let mut s = rng.gen();
Expand Down
1 change: 1 addition & 0 deletions src/geometry/translation_construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ where
DefaultAllocator: Allocator<N, D>,
Standard: Distribution<N>,
{
/// Generate an arbitrary random variate for testing purposes.
#[inline]
fn sample<'a, G: Rng + ?Sized>(&self, rng: &'a mut G) -> Translation<N, D> {
Translation::from(rng.gen::<VectorN<N, D>>())
Expand Down
10 changes: 4 additions & 6 deletions src/geometry/unit_complex_construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
use quickcheck::{Arbitrary, Gen};

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

use num::One;
use num_complex::Complex;
Expand Down Expand Up @@ -401,12 +398,13 @@ where
impl<N: SimdRealField> Distribution<UnitComplex<N>> for Standard
where
N::Element: SimdRealField,
OpenClosed01: Distribution<N>,
rand_distr::UnitCircle: Distribution<[N; 2]>,
{
/// Generate a uniformly distributed random `UnitComplex`.
#[inline]
fn sample<'a, R: Rng + ?Sized>(&self, rng: &mut R) -> UnitComplex<N> {
UnitComplex::from_angle(rng.sample(OpenClosed01) * N::simd_two_pi())
let x = rng.sample(rand_distr::UnitCircle);
UnitComplex::new_unchecked(Complex::new(x[0], x[1]))
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/core/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ where

// This is a wrapper similar to RandComplex, but for non-complex.
// This exists only to make generic tests easier to write.
//
// Generates variates in the range [0, 1).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct RandScalar<N>(pub N);

Expand Down