summaryrefslogtreecommitdiff
path: root/rand/src/distributions
diff options
context:
space:
mode:
Diffstat (limited to 'rand/src/distributions')
-rw-r--r--rand/src/distributions/integer.rs8
-rw-r--r--rand/src/distributions/mod.rs6
-rw-r--r--rand/src/distributions/uniform.rs31
-rw-r--r--rand/src/distributions/unit_circle.rs11
-rw-r--r--rand/src/distributions/unit_sphere.rs9
-rw-r--r--rand/src/distributions/utils.rs6
6 files changed, 35 insertions, 36 deletions
diff --git a/rand/src/distributions/integer.rs b/rand/src/distributions/integer.rs
index 4e6604d..7e408db 100644
--- a/rand/src/distributions/integer.rs
+++ b/rand/src/distributions/integer.rs
@@ -45,7 +45,7 @@ impl Distribution<u64> for Standard {
}
}
-#[cfg(rust_1_26)]
+#[cfg(all(rustc_1_26, not(target_os = "emscripten")))]
impl Distribution<u128> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u128 {
@@ -85,7 +85,7 @@ impl_int_from_uint! { i8, u8 }
impl_int_from_uint! { i16, u16 }
impl_int_from_uint! { i32, u32 }
impl_int_from_uint! { i64, u64 }
-#[cfg(rust_1_26)] impl_int_from_uint! { i128, u128 }
+#[cfg(all(rustc_1_26, not(target_os = "emscripten")))] impl_int_from_uint! { i128, u128 }
impl_int_from_uint! { isize, usize }
#[cfg(feature="simd_support")]
@@ -147,7 +147,7 @@ mod tests {
rng.sample::<i16, _>(Standard);
rng.sample::<i32, _>(Standard);
rng.sample::<i64, _>(Standard);
- #[cfg(rust_1_26)]
+ #[cfg(all(rustc_1_26, not(target_os = "emscripten")))]
rng.sample::<i128, _>(Standard);
rng.sample::<usize, _>(Standard);
@@ -155,7 +155,7 @@ mod tests {
rng.sample::<u16, _>(Standard);
rng.sample::<u32, _>(Standard);
rng.sample::<u64, _>(Standard);
- #[cfg(rust_1_26)]
+ #[cfg(all(rustc_1_26, not(target_os = "emscripten")))]
rng.sample::<u128, _>(Standard);
}
}
diff --git a/rand/src/distributions/mod.rs b/rand/src/distributions/mod.rs
index 160cd31..5e879cb 100644
--- a/rand/src/distributions/mod.rs
+++ b/rand/src/distributions/mod.rs
@@ -182,7 +182,7 @@
//! [`Weibull`]: struct.Weibull.html
//! [`WeightedIndex`]: struct.WeightedIndex.html
-#[cfg(any(rust_1_26, features="nightly"))]
+#[cfg(any(rustc_1_26, features="nightly"))]
use core::iter;
use Rng;
@@ -316,7 +316,7 @@ impl<'a, D, R, T> Iterator for DistIter<'a, D, R, T>
}
}
-#[cfg(rust_1_26)]
+#[cfg(rustc_1_26)]
impl<'a, D, R, T> iter::FusedIterator for DistIter<'a, D, R, T>
where D: Distribution<T>, R: Rng + 'a {}
@@ -328,7 +328,7 @@ impl<'a, D, R, T> iter::TrustedLen for DistIter<'a, D, R, T>
/// A generic random value distribution, implemented for many primitive types.
/// Usually generates values with a numerically uniform distribution, and with a
/// range appropriate to the type.
-///
+///
/// ## Built-in Implementations
///
/// Assuming the provided `Rng` is well-behaved, these implementations
diff --git a/rand/src/distributions/uniform.rs b/rand/src/distributions/uniform.rs
index 5fb89e3..ceed77d 100644
--- a/rand/src/distributions/uniform.rs
+++ b/rand/src/distributions/uniform.rs
@@ -111,7 +111,7 @@
#[cfg(feature = "std")]
use std::time::Duration;
-#[cfg(all(not(feature = "std"), rust_1_25))]
+#[cfg(all(not(feature = "std"), rustc_1_25))]
use core::time::Duration;
use Rng;
@@ -277,7 +277,7 @@ impl<X: SampleUniform> From<::core::ops::Range<X>> for Uniform<X> {
}
}
-#[cfg(rust_1_27)]
+#[cfg(rustc_1_27)]
impl<X: SampleUniform> From<::core::ops::RangeInclusive<X>> for Uniform<X> {
fn from(r: ::core::ops::RangeInclusive<X>) -> Uniform<X> {
Uniform::new_inclusive(r.start(), r.end())
@@ -452,8 +452,9 @@ macro_rules! uniform_int_impl {
let ints_to_reject = (unsigned_max - range + 1) % range;
unsigned_max - ints_to_reject
} else {
- // conservative but fast approximation
- range << range.leading_zeros()
+ // conservative but fast approximation. `- 1` is necessary to allow the
+ // same comparison without bias.
+ (range << range.leading_zeros()).wrapping_sub(1)
};
loop {
@@ -472,7 +473,7 @@ uniform_int_impl! { i8, i8, u8, i32, u32 }
uniform_int_impl! { i16, i16, u16, i32, u32 }
uniform_int_impl! { i32, i32, u32, i32, u32 }
uniform_int_impl! { i64, i64, u64, i64, u64 }
-#[cfg(rust_1_26)]
+#[cfg(all(rustc_1_26, not(target_os = "emscripten")))]
uniform_int_impl! { i128, i128, u128, u128, u128 }
uniform_int_impl! { isize, isize, usize, isize, usize }
uniform_int_impl! { u8, i8, u8, i32, u32 }
@@ -480,7 +481,7 @@ uniform_int_impl! { u16, i16, u16, i32, u32 }
uniform_int_impl! { u32, i32, u32, i32, u32 }
uniform_int_impl! { u64, i64, u64, i64, u64 }
uniform_int_impl! { usize, isize, usize, isize, usize }
-#[cfg(rust_1_26)]
+#[cfg(all(rustc_1_26, not(target_os = "emscripten")))]
uniform_int_impl! { u128, u128, u128, i128, u128 }
#[cfg(all(feature = "simd_support", feature = "nightly"))]
@@ -835,14 +836,14 @@ uniform_float_impl! { f64x8, u64x8, f64, u64, 64 - 52 }
///
/// [`UniformSampler`]: trait.UniformSampler.html
/// [`Uniform`]: struct.Uniform.html
-#[cfg(any(feature = "std", rust_1_25))]
+#[cfg(any(feature = "std", rustc_1_25))]
#[derive(Clone, Copy, Debug)]
pub struct UniformDuration {
mode: UniformDurationMode,
offset: u32,
}
-#[cfg(any(feature = "std", rust_1_25))]
+#[cfg(any(feature = "std", rustc_1_25))]
#[derive(Debug, Copy, Clone)]
enum UniformDurationMode {
Small {
@@ -859,12 +860,12 @@ enum UniformDurationMode {
}
}
-#[cfg(any(feature = "std", rust_1_25))]
+#[cfg(any(feature = "std", rustc_1_25))]
impl SampleUniform for Duration {
type Sampler = UniformDuration;
}
-#[cfg(any(feature = "std", rust_1_25))]
+#[cfg(any(feature = "std", rustc_1_25))]
impl UniformSampler for UniformDuration {
type X = Duration;
@@ -989,7 +990,7 @@ mod tests {
fn test_integers() {
use core::{i8, i16, i32, i64, isize};
use core::{u8, u16, u32, u64, usize};
- #[cfg(rust_1_26)]
+ #[cfg(all(rustc_1_26, not(target_os = "emscripten")))]
use core::{i128, u128};
let mut rng = ::test::rng(251);
@@ -1053,7 +1054,7 @@ mod tests {
}
t!(i8, i16, i32, i64, isize,
u8, u16, u32, u64, usize);
- #[cfg(rust_1_26)]
+ #[cfg(all(rustc_1_26, not(target_os = "emscripten")))]
t!(i128, u128);
#[cfg(all(feature = "simd_support", feature = "nightly"))]
@@ -1208,11 +1209,11 @@ mod tests {
#[test]
- #[cfg(any(feature = "std", rust_1_25))]
+ #[cfg(any(feature = "std", rustc_1_25))]
fn test_durations() {
#[cfg(feature = "std")]
use std::time::Duration;
- #[cfg(all(not(feature = "std"), rust_1_25))]
+ #[cfg(all(not(feature = "std"), rustc_1_25))]
use core::time::Duration;
let mut rng = ::test::rng(253);
@@ -1283,7 +1284,7 @@ mod tests {
assert_eq!(r.inner.scale, 5.0);
}
- #[cfg(rust_1_27)]
+ #[cfg(rustc_1_27)]
#[test]
fn test_uniform_from_std_range_inclusive() {
let r = Uniform::from(2u32..=6);
diff --git a/rand/src/distributions/unit_circle.rs b/rand/src/distributions/unit_circle.rs
index abb36dc..01ab76a 100644
--- a/rand/src/distributions/unit_circle.rs
+++ b/rand/src/distributions/unit_circle.rs
@@ -29,27 +29,26 @@ use distributions::{Distribution, Uniform};
/// NBS Appl. Math. Ser., No. 12. Washington, DC: U.S. Government Printing
/// Office, pp. 36-38.
#[derive(Clone, Copy, Debug)]
-pub struct UnitCircle {
- uniform: Uniform<f64>,
-}
+pub struct UnitCircle;
impl UnitCircle {
/// Construct a new `UnitCircle` distribution.
#[inline]
pub fn new() -> UnitCircle {
- UnitCircle { uniform: Uniform::new(-1., 1.) }
+ UnitCircle
}
}
impl Distribution<[f64; 2]> for UnitCircle {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> [f64; 2] {
+ let uniform = Uniform::new(-1., 1.);
let mut x1;
let mut x2;
let mut sum;
loop {
- x1 = self.uniform.sample(rng);
- x2 = self.uniform.sample(rng);
+ x1 = uniform.sample(rng);
+ x2 = uniform.sample(rng);
sum = x1*x1 + x2*x2;
if sum < 1. {
break;
diff --git a/rand/src/distributions/unit_sphere.rs b/rand/src/distributions/unit_sphere.rs
index 61cbda5..37de88b 100644
--- a/rand/src/distributions/unit_sphere.rs
+++ b/rand/src/distributions/unit_sphere.rs
@@ -28,23 +28,22 @@ use distributions::{Distribution, Uniform};
/// Sphere.*](https://doi.org/10.1214/aoms/1177692644)
/// Ann. Math. Statist. 43, no. 2, 645--646.
#[derive(Clone, Copy, Debug)]
-pub struct UnitSphereSurface {
- uniform: Uniform<f64>,
-}
+pub struct UnitSphereSurface;
impl UnitSphereSurface {
/// Construct a new `UnitSphereSurface` distribution.
#[inline]
pub fn new() -> UnitSphereSurface {
- UnitSphereSurface { uniform: Uniform::new(-1., 1.) }
+ UnitSphereSurface
}
}
impl Distribution<[f64; 3]> for UnitSphereSurface {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> [f64; 3] {
+ let uniform = Uniform::new(-1., 1.);
loop {
- let (x1, x2) = (self.uniform.sample(rng), self.uniform.sample(rng));
+ let (x1, x2) = (uniform.sample(rng), uniform.sample(rng));
let sum = x1*x1 + x2*x2;
if sum >= 1. {
continue;
diff --git a/rand/src/distributions/utils.rs b/rand/src/distributions/utils.rs
index a2112fd..d4d3642 100644
--- a/rand/src/distributions/utils.rs
+++ b/rand/src/distributions/utils.rs
@@ -61,7 +61,7 @@ macro_rules! wmul_impl {
wmul_impl! { u8, u16, 8 }
wmul_impl! { u16, u32, 16 }
wmul_impl! { u32, u64, 32 }
-#[cfg(rust_1_26)]
+#[cfg(all(rustc_1_26, not(target_os = "emscripten")))]
wmul_impl! { u64, u128, 64 }
// This code is a translation of the __mulddi3 function in LLVM's
@@ -125,9 +125,9 @@ macro_rules! wmul_impl_large {
)+
};
}
-#[cfg(not(rust_1_26))]
+#[cfg(not(all(rustc_1_26, not(target_os = "emscripten"))))]
wmul_impl_large! { u64, 32 }
-#[cfg(rust_1_26)]
+#[cfg(all(rustc_1_26, not(target_os = "emscripten")))]
wmul_impl_large! { u128, 64 }
macro_rules! wmul_impl_usize {