Debian Codegraph @

src/distributions/uniform.rs

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 // Copyright 2018-2020 Developers of the Rand project.
2 // Copyright 2017 The Rust Project Developers.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 //! A distribution uniformly sampling numbers within a given range.
11 //!
12 //! [`Uniform`] is the standard distribution to sample uniformly from a range;
13 //! e.g. `Uniform::new_inclusive(1, 6)` can sample integers from 1 to 6, like a
14 //! standard die. [`Rng::gen_range`] supports any type supported by
15 //! [`Uniform`].
16 //!
17 //! This distribution is provided with support for several primitive types
18 //! (all integer and floating-point types) as well as [`std::time::Duration`],
19 //! and supports extension to user-defined types via a type-specific *back-end*
20 //! implementation.
21 //!
22 //! The types [`UniformInt`], [`UniformFloat`] and [`UniformDuration`] are the
23 //! back-ends supporting sampling from primitive integer and floating-point
24 //! ranges as well as from [`std::time::Duration`]; these types do not normally
25 //! need to be used directly (unless implementing a derived back-end).
26 //!
27 //! # Example usage
28 //!
29 //! ```
30 //! use rand::{Rng, thread_rng};
31 //! use rand::distributions::Uniform;
32 //!
33 //! let mut rng = thread_rng();
34 //! let side = Uniform::new(-10.0, 10.0);
35 //!
36 //! // sample between 1 and 10 points
37 //! for _ in 0..rng.gen_range(1..=10) {
38 //! // sample a point from the square with sides -10 - 10 in two dimensions
39 //! let (x, y) = (rng.sample(side), rng.sample(side));
40 //! println!("Point: {}, {}", x, y);
41 //! }
42 //! ```
43 //!
44 //! # Extending `Uniform` to support a custom type
45 //!
46 //! To extend [`Uniform`] to support your own types, write a back-end which
47 //! implements the [`UniformSampler`] trait, then implement the [`SampleUniform`]
48 //! helper trait to "register" your back-end. See the `MyF32` example below.
49 //!
50 //! At a minimum, the back-end needs to store any parameters needed for sampling
51 //! (e.g. the target range) and implement `new`, `new_inclusive` and `sample`.
52 //! Those methods should include an assert to check the range is valid (i.e.
53 //! `low < high`). The example below merely wraps another back-end.
54 //!
55 //! The `new`, `new_inclusive` and `sample_single` functions use arguments of
56 //! type `SampleBorrow<X>` in order to support passing in values by reference or
57 //! by value. In the implementation of these functions, you can choose to
58 //! simply use the reference returned by [`SampleBorrow::borrow`], or you can choose
59 //! to copy or clone the value, whatever is appropriate for your type.
60 //!
61 //! ```
62 //! use rand::prelude::*;
63 //! use rand::distributions::uniform::{Uniform, SampleUniform,
64 //! UniformSampler, UniformFloat, SampleBorrow};
65 //!
66 //! struct MyF32(f32);
67 //!
68 //! #[derive(Clone, Copy, Debug)]
69 //! struct UniformMyF32(UniformFloat<f32>);
70 //!
71 //! impl UniformSampler for UniformMyF32 {
72 //! type X = MyF32;
73 //! fn new<B1, B2>(low: B1, high: B2) -> Self
74 //! where B1: SampleBorrow<Self::X> + Sized,
75 //! B2: SampleBorrow<Self::X> + Sized
76 //! {
77 //! UniformMyF32(UniformFloat::<f32>::new(low.borrow().0, high.borrow().0))
78 //! }
79 //! fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self
80 //! where B1: SampleBorrow<Self::X> + Sized,
81 //! B2: SampleBorrow<Self::X> + Sized
82 //! {
83 //! UniformMyF32(UniformFloat::<f32>::new_inclusive(
84 //! low.borrow().0,
85 //! high.borrow().0,
86 //! ))
87 //! }
88 //! fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
89 //! MyF32(self.0.sample(rng))
90 //! }
91 //! }
92 //!
93 //! impl SampleUniform for MyF32 {
94 //! type Sampler = UniformMyF32;
95 //! }
96 //!
97 //! let (low, high) = (MyF32(17.0f32), MyF32(22.0f32));
98 //! let uniform = Uniform::new(low, high);
99 //! let x = uniform.sample(&mut thread_rng());
100 //! ```
101 //!
102 //! [`SampleUniform`]: crate::distributions::uniform::SampleUniform
103 //! [`UniformSampler`]: crate::distributions::uniform::UniformSampler
104 //! [`UniformInt`]: crate::distributions::uniform::UniformInt
105 //! [`UniformFloat`]: crate::distributions::uniform::UniformFloat
106 //! [`UniformDuration`]: crate::distributions::uniform::UniformDuration
107 //! [`SampleBorrow::borrow`]: crate::distributions::uniform::SampleBorrow::borrow
108
109 use core::time::Duration;
110 use core::ops::{Range, RangeInclusive};
111
112 use crate::distributions::float::IntoFloat;
113 use crate::distributions::utils::{BoolAsSIMD, FloatAsSIMD, FloatSIMDUtils, WideningMultiply};
114 use crate::distributions::Distribution;
115 use crate::{Rng, RngCore};
116
117 #[cfg(not(feature = "std"))]
118 #[allow(unused_imports)] // rustc doesn't detect that this is actually used
119 use crate::distributions::utils::Float;
120
121 #[cfg(feature = "serde1")]
122 use serde::{Serialize, Deserialize};
123
124 /// Sample values uniformly between two bounds.
125 ///
126 /// [`Uniform::new`] and [`Uniform::new_inclusive`] construct a uniform
127 /// distribution sampling from the given range; these functions may do extra
128 /// work up front to make sampling of multiple values faster. If only one sample
129 /// from the range is required, [`Rng::gen_range`] can be more efficient.
130 ///
131 /// When sampling from a constant range, many calculations can happen at
132 /// compile-time and all methods should be fast; for floating-point ranges and
133 /// the full range of integer types this should have comparable performance to
134 /// the `Standard` distribution.
135 ///
136 /// Steps are taken to avoid bias which might be present in naive
137 /// implementations; for example `rng.gen::<u8>() % 170` samples from the range
138 /// `[0, 169]` but is twice as likely to select numbers less than 85 than other
139 /// values. Further, the implementations here give more weight to the high-bits
140 /// generated by the RNG than the low bits, since with some RNGs the low-bits
141 /// are of lower quality than the high bits.
142 ///
143 /// Implementations must sample in `[low, high)` range for
144 /// `Uniform::new(low, high)`, i.e., excluding `high`. In particular, care must
145 /// be taken to ensure that rounding never results values `< low` or `>= high`.
146 ///
147 /// # Example
148 ///
149 /// ```
150 /// use rand::distributions::{Distribution, Uniform};
151 ///
152 /// let between = Uniform::from(10..10000);
153 /// let mut rng = rand::thread_rng();
154 /// let mut sum = 0;
155 /// for _ in 0..1000 {
156 /// sum += between.sample(&mut rng);
157 /// }
158 /// println!("{}", sum);
159 /// ```
160 ///
161 /// For a single sample, [`Rng::gen_range`] may be preferred:
162 ///
163 /// ```
164 /// use rand::Rng;
165 ///
166 /// let mut rng = rand::thread_rng();
167 /// println!("{}", rng.gen_range(0..10));
168 /// ```
169 ///
170 /// [`new`]: Uniform::new
171 /// [`new_inclusive`]: Uniform::new_inclusive
172 /// [`Rng::gen_range`]: Rng::gen_range
173 #[derive(Clone, Copy, Debug, PartialEq)]
174 #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
175 #[cfg_attr(feature = "serde1", serde(bound(serialize = "X::Sampler: Serialize")))]
176 #[cfg_attr(feature = "serde1", serde(bound(deserialize = "X::Sampler: Deserialize<'de>")))]
177 pub struct Uniform<X: SampleUniform>(X::Sampler);
178
179 impl<X: SampleUniform> Uniform<X> {
180 /// Create a new `Uniform` instance which samples uniformly from the half
181 /// open range `[low, high)` (excluding `high`). Panics if `low >= high`.
182 pub fn new<B1, B2>(low: B1, high: B2) -> Uniform<X>
183 where
184 B1: SampleBorrow<X> + Sized,
185 B2: SampleBorrow<X> + Sized,
186 {
187 Uniform(X::Sampler::new(low, high))
188 }
189
190 /// Create a new `Uniform` instance which samples uniformly from the closed
191 /// range `[low, high]` (inclusive). Panics if `low > high`.
192 pub fn new_inclusive<B1, B2>(low: B1, high: B2) -> Uniform<X>
193 where
194 B1: SampleBorrow<X> + Sized,
195 B2: SampleBorrow<X> + Sized,
196 {
197 Uniform(X::Sampler::new_inclusive(low, high))
198 }
199 }
200
201 impl<X: SampleUniform> Distribution<X> for Uniform<X> {
202 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> X {
203 self.0.sample(rng)
204 }
205 }
206
207 /// Helper trait for creating objects using the correct implementation of
208 /// [`UniformSampler`] for the sampling type.
209 ///
210 /// See the [module documentation] on how to implement [`Uniform`] range
211 /// sampling for a custom type.
212 ///
213 /// [module documentation]: crate::distributions::uniform
214 pub trait SampleUniform: Sized {
215 /// The `UniformSampler` implementation supporting type `X`.
216 type Sampler: UniformSampler<X = Self>;
217 }
218
219 /// Helper trait handling actual uniform sampling.
220 ///
221 /// See the [module documentation] on how to implement [`Uniform`] range
222 /// sampling for a custom type.
223 ///
224 /// Implementation of [`sample_single`] is optional, and is only useful when
225 /// the implementation can be faster than `Self::new(low, high).sample(rng)`.
226 ///
227 /// [module documentation]: crate::distributions::uniform
228 /// [`sample_single`]: UniformSampler::sample_single
229 pub trait UniformSampler: Sized {
230 /// The type sampled by this implementation.
231 type X;
232
233 /// Construct self, with inclusive lower bound and exclusive upper bound
234 /// `[low, high)`.
235 ///
236 /// Usually users should not call this directly but instead use
237 /// `Uniform::new`, which asserts that `low < high` before calling this.
238 fn new<B1, B2>(low: B1, high: B2) -> Self
239 where
240 B1: SampleBorrow<Self::X> + Sized,
241 B2: SampleBorrow<Self::X> + Sized;
242
243 /// Construct self, with inclusive bounds `[low, high]`.
244 ///
245 /// Usually users should not call this directly but instead use
246 /// `Uniform::new_inclusive`, which asserts that `low <= high` before
247 /// calling this.
248 fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self
249 where
250 B1: SampleBorrow<Self::X> + Sized,
251 B2: SampleBorrow<Self::X> + Sized;
252
253 /// Sample a value.
254 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X;
255
256 /// Sample a single value uniformly from a range with inclusive lower bound
257 /// and exclusive upper bound `[low, high)`.
258 ///
259 /// By default this is implemented using
260 /// `UniformSampler::new(low, high).sample(rng)`. However, for some types
261 /// more optimal implementations for single usage may be provided via this
262 /// method (which is the case for integers and floats).
263 /// Results may not be identical.
264 ///
265 /// Note that to use this method in a generic context, the type needs to be
266 /// retrieved via `SampleUniform::Sampler` as follows:
267 /// ```
268 /// use rand::{thread_rng, distributions::uniform::{SampleUniform, UniformSampler}};
269 /// # #[allow(unused)]
270 /// fn sample_from_range<T: SampleUniform>(lb: T, ub: T) -> T {
271 /// let mut rng = thread_rng();
272 /// <T as SampleUniform>::Sampler::sample_single(lb, ub, &mut rng)
273 /// }
274 /// ```
275 fn sample_single<R: Rng + ?Sized, B1, B2>(low: B1, high: B2, rng: &mut R) -> Self::X
276 where
277 B1: SampleBorrow<Self::X> + Sized,
278 B2: SampleBorrow<Self::X> + Sized,
279 {
280 let uniform: Self = UniformSampler::new(low, high);
281 uniform.sample(rng)
282 }
283
284 /// Sample a single value uniformly from a range with inclusive lower bound
285 /// and inclusive upper bound `[low, high]`.
286 ///
287 /// By default this is implemented using
288 /// `UniformSampler::new_inclusive(low, high).sample(rng)`. However, for
289 /// some types more optimal implementations for single usage may be provided
290 /// via this method.
291 /// Results may not be identical.
292 fn sample_single_inclusive<R: Rng + ?Sized, B1, B2>(low: B1, high: B2, rng: &mut R)
293 -> Self::X
294 where B1: SampleBorrow<Self::X> + Sized,
295 B2: SampleBorrow<Self::X> + Sized
296 {
297 let uniform: Self = UniformSampler::new_inclusive(low, high);
298 uniform.sample(rng)
299 }
300 }
301
302 impl<X: SampleUniform> From<Range<X>> for Uniform<X> {
303 fn from(r: ::core::ops::Range<X>) -> Uniform<X> {
304 Uniform::new(r.start, r.end)
305 }
306 }
307
308 impl<X: SampleUniform> From<RangeInclusive<X>> for Uniform<X> {
309 fn from(r: ::core::ops::RangeInclusive<X>) -> Uniform<X> {
310 Uniform::new_inclusive(r.start(), r.end())
311 }
312 }
313
314
315 /// Helper trait similar to [`Borrow`] but implemented
316 /// only for SampleUniform and references to SampleUniform in
317 /// order to resolve ambiguity issues.
318 ///
319 /// [`Borrow`]: std::borrow::Borrow
320 pub trait SampleBorrow<Borrowed> {
321 /// Immutably borrows from an owned value. See [`Borrow::borrow`]
322 ///
323 /// [`Borrow::borrow`]: std::borrow::Borrow::borrow
324 fn borrow(&self) -> &Borrowed;
325 }
326 impl<Borrowed> SampleBorrow<Borrowed> for Borrowed
327 where Borrowed: SampleUniform
328 {
329 #[inline(always)]
330 fn borrow(&self) -> &Borrowed {
331 self
332 }
333 }
334 impl<'a, Borrowed> SampleBorrow<Borrowed> for &'a Borrowed
335 where Borrowed: SampleUniform
336 {
337 #[inline(always)]
338 fn borrow(&self) -> &Borrowed {
339 *self
340 }
341 }
342
343 /// Range that supports generating a single sample efficiently.
344 ///
345 /// Any type implementing this trait can be used to specify the sampled range
346 /// for `Rng::gen_range`.
347 pub trait SampleRange<T> {
348 /// Generate a sample from the given range.
349 fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T;
350
351 /// Check whether the range is empty.
352 fn is_empty(&self) -> bool;
353 }
354
355 impl<T: SampleUniform + PartialOrd> SampleRange<T> for Range<T> {
356 #[inline]
357 fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T {
358 T::Sampler::sample_single(self.start, self.end, rng)
359 }
360
361 #[inline]
362 fn is_empty(&self) -> bool {
363 !(self.start < self.end)
364 }
365 }
366
367 impl<T: SampleUniform + PartialOrd> SampleRange<T> for RangeInclusive<T> {
368 #[inline]
369 fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T {
370 T::Sampler::sample_single_inclusive(self.start(), self.end(), rng)
371 }
372
373 #[inline]
374 fn is_empty(&self) -> bool {
375 !(self.start() <= self.end())
376 }
377 }
378
379
380 ////////////////////////////////////////////////////////////////////////////////
381
382 // What follows are all back-ends.
383
384
385 /// The back-end implementing [`UniformSampler`] for integer types.
386 ///
387 /// Unless you are implementing [`UniformSampler`] for your own type, this type
388 /// should not be used directly, use [`Uniform`] instead.
389 ///
390 /// # Implementation notes
391 ///
392 /// For simplicity, we use the same generic struct `UniformInt<X>` for all
393 /// integer types `X`. This gives us only one field type, `X`; to store unsigned
394 /// values of this size, we take use the fact that these conversions are no-ops.
395 ///
396 /// For a closed range, the number of possible numbers we should generate is
397 /// `range = (high - low + 1)`. To avoid bias, we must ensure that the size of
398 /// our sample space, `zone`, is a multiple of `range`; other values must be
399 /// rejected (by replacing with a new random sample).
400 ///
401 /// As a special case, we use `range = 0` to represent the full range of the
402 /// result type (i.e. for `new_inclusive($ty::MIN, $ty::MAX)`).
403 ///
404 /// The optimum `zone` is the largest product of `range` which fits in our
405 /// (unsigned) target type. We calculate this by calculating how many numbers we
406 /// must reject: `reject = (MAX + 1) % range = (MAX - range + 1) % range`. Any (large)
407 /// product of `range` will suffice, thus in `sample_single` we multiply by a
408 /// power of 2 via bit-shifting (faster but may cause more rejections).
409 ///
410 /// The smallest integer PRNGs generate is `u32`. For 8- and 16-bit outputs we
411 /// use `u32` for our `zone` and samples (because it's not slower and because
412 /// it reduces the chance of having to reject a sample). In this case we cannot
413 /// store `zone` in the target type since it is too large, however we know
414 /// `ints_to_reject < range <= $unsigned::MAX`.
415 ///
416 /// An alternative to using a modulus is widening multiply: After a widening
417 /// multiply by `range`, the result is in the high word. Then comparing the low
418 /// word against `zone` makes sure our distribution is uniform.
419 #[derive(Clone, Copy, Debug, PartialEq)]
420 #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
421 pub struct UniformInt<X> {
422 low: X,
423 range: X,
424 z: X, // either ints_to_reject or zone depending on implementation
425 }
426
427 macro_rules! uniform_int_impl {
428 ($ty:ty, $unsigned:ident, $u_large:ident) => {
429 impl SampleUniform for $ty {
430 type Sampler = UniformInt<$ty>;
431 }
432
433 impl UniformSampler for UniformInt<$ty> {
434 // We play free and fast with unsigned vs signed here
435 // (when $ty is signed), but that's fine, since the
436 // contract of this macro is for $ty and $unsigned to be
437 // "bit-equal", so casting between them is a no-op.
438
439 type X = $ty;
440
441 #[inline] // if the range is constant, this helps LLVM to do the
442 // calculations at compile-time.
443 fn new<B1, B2>(low_b: B1, high_b: B2) -> Self
444 where
445 B1: SampleBorrow<Self::X> + Sized,
446 B2: SampleBorrow<Self::X> + Sized,
447 {
448 let low = *low_b.borrow();
449 let high = *high_b.borrow();
450 assert!(low < high, "Uniform::new called with `low >= high`");
451 UniformSampler::new_inclusive(low, high - 1)
452 }
453
454 #[inline] // if the range is constant, this helps LLVM to do the
455 // calculations at compile-time.
456 fn new_inclusive<B1, B2>(low_b: B1, high_b: B2) -> Self
457 where
458 B1: SampleBorrow<Self::X> + Sized,
459 B2: SampleBorrow<Self::X> + Sized,
460 {
461 let low = *low_b.borrow();
462 let high = *high_b.borrow();
463 assert!(
464 low <= high,
465 "Uniform::new_inclusive called with `low > high`"
466 );
467 let unsigned_max = ::core::$u_large::MAX;
468
469 let range = high.wrapping_sub(low).wrapping_add(1) as $unsigned;
470 let ints_to_reject = if range > 0 {
471 let range = $u_large::from(range);
472 (unsigned_max - range + 1) % range
473 } else {
474 0
475 };
476
477 UniformInt {
478 low,
479 // These are really $unsigned values, but store as $ty:
480 range: range as $ty,
481 z: ints_to_reject as $unsigned as $ty,
482 }
483 }
484
485 #[inline]
486 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
487 let range = self.range as $unsigned as $u_large;
488 if range > 0 {
489 let unsigned_max = ::core::$u_large::MAX;
490 let zone = unsigned_max - (self.z as $unsigned as $u_large);
491 loop {
492 let v: $u_large = rng.gen();
493 let (hi, lo) = v.wmul(range);
494 if lo <= zone {
495 return self.low.wrapping_add(hi as $ty);
496 }
497 }
498 } else {
499 // Sample from the entire integer range.
500 rng.gen()
501 }
502 }
503
504 #[inline]
505 fn sample_single<R: Rng + ?Sized, B1, B2>(low_b: B1, high_b: B2, rng: &mut R) -> Self::X
506 where
507 B1: SampleBorrow<Self::X> + Sized,
508 B2: SampleBorrow<Self::X> + Sized,
509 {
510 let low = *low_b.borrow();
511 let high = *high_b.borrow();
512 assert!(low < high, "UniformSampler::sample_single: low >= high");
513 Self::sample_single_inclusive(low, high - 1, rng)
514 }
515
516 #[inline]
517 fn sample_single_inclusive<R: Rng + ?Sized, B1, B2>(low_b: B1, high_b: B2, rng: &mut R) -> Self::X
518 where
519 B1: SampleBorrow<Self::X> + Sized,
520 B2: SampleBorrow<Self::X> + Sized,
521 {
522 let low = *low_b.borrow();
523 let high = *high_b.borrow();
524 assert!(low <= high, "UniformSampler::sample_single_inclusive: low > high");
525 let range = high.wrapping_sub(low).wrapping_add(1) as $unsigned as $u_large;
526 // If the above resulted in wrap-around to 0, the range is $ty::MIN..=$ty::MAX,
527 // and any integer will do.
528 if range == 0 {
529 return rng.gen();
530 }
531
532 let zone = if ::core::$unsigned::MAX <= ::core::u16::MAX as $unsigned {
533 // Using a modulus is faster than the approximation for
534 // i8 and i16. I suppose we trade the cost of one
535 // modulus for near-perfect branch prediction.
536 let unsigned_max: $u_large = ::core::$u_large::MAX;
537 let ints_to_reject = (unsigned_max - range + 1) % range;
538 unsigned_max - ints_to_reject
539 } else {
540 // conservative but fast approximation. `- 1` is necessary to allow the
541 // same comparison without bias.
542 (range << range.leading_zeros()).wrapping_sub(1)
543 };
544
545 loop {
546 let v: $u_large = rng.gen();
547 let (hi, lo) = v.wmul(range);
548 if lo <= zone {
549 return low.wrapping_add(hi as $ty);
550 }
551 }
552 }
553 }
554 };
555 }
556
557 uniform_int_impl! { i8, u8, u32 }
558 uniform_int_impl! { i16, u16, u32 }
559 uniform_int_impl! { i32, u32, u32 }
560 uniform_int_impl! { i64, u64, u64 }
561 uniform_int_impl! { i128, u128, u128 }
562 uniform_int_impl! { isize, usize, usize }
563 uniform_int_impl! { u8, u8, u32 }
564 uniform_int_impl! { u16, u16, u32 }
565 uniform_int_impl! { u32, u32, u32 }
566 uniform_int_impl! { u64, u64, u64 }
567 uniform_int_impl! { usize, usize, usize }
568 uniform_int_impl! { u128, u128, u128 }
569
570 impl SampleUniform for char {
571 type Sampler = UniformChar;
572 }
573
574 /// The back-end implementing [`UniformSampler`] for `char`.
575 ///
576 /// Unless you are implementing [`UniformSampler`] for your own type, this type
577 /// should not be used directly, use [`Uniform`] instead.
578 ///
579 /// This differs from integer range sampling since the range `0xD800..=0xDFFF`
580 /// are used for surrogate pairs in UCS and UTF-16, and consequently are not
581 /// valid Unicode code points. We must therefore avoid sampling values in this
582 /// range.
583 #[derive(Clone, Copy, Debug)]
584 #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
585 pub struct UniformChar {
586 sampler: UniformInt<u32>,
587 }
588
589 /// UTF-16 surrogate range start
590 const CHAR_SURROGATE_START: u32 = 0xD800;
591 /// UTF-16 surrogate range size
592 const CHAR_SURROGATE_LEN: u32 = 0xE000 - CHAR_SURROGATE_START;
593
594 /// Convert `char` to compressed `u32`
595 fn char_to_comp_u32(c: char) -> u32 {
596 match c as u32 {
597 c if c >= CHAR_SURROGATE_START => c - CHAR_SURROGATE_LEN,
598 c => c,
599 }
600 }
601
602 impl UniformSampler for UniformChar {
603 type X = char;
604
605 #[inline] // if the range is constant, this helps LLVM to do the
606 // calculations at compile-time.
607 fn new<B1, B2>(low_b: B1, high_b: B2) -> Self
608 where
609 B1: SampleBorrow<Self::X> + Sized,
610 B2: SampleBorrow<Self::X> + Sized,
611 {
612 let low = char_to_comp_u32(*low_b.borrow());
613 let high = char_to_comp_u32(*high_b.borrow());
614 let sampler = UniformInt::<u32>::new(low, high);
615 UniformChar { sampler }
616 }
617
618 #[inline] // if the range is constant, this helps LLVM to do the
619 // calculations at compile-time.
620 fn new_inclusive<B1, B2>(low_b: B1, high_b: B2) -> Self
621 where
622 B1: SampleBorrow<Self::X> + Sized,
623 B2: SampleBorrow<Self::X> + Sized,
624 {
625 let low = char_to_comp_u32(*low_b.borrow());
626 let high = char_to_comp_u32(*high_b.borrow());
627 let sampler = UniformInt::<u32>::new_inclusive(low, high);
628 UniformChar { sampler }
629 }
630
631 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
632 let mut x = self.sampler.sample(rng);
633 if x >= CHAR_SURROGATE_START {
634 x += CHAR_SURROGATE_LEN;
635 }
636 // SAFETY: x must not be in surrogate range or greater than char::MAX.
637 // This relies on range constructors which accept char arguments.
638 // Validity of input char values is assumed.
639 unsafe { core::char::from_u32_unchecked(x) }
640 }
641 }
642
643 /// The back-end implementing [`UniformSampler`] for floating-point types.
644 ///
645 /// Unless you are implementing [`UniformSampler`] for your own type, this type
646 /// should not be used directly, use [`Uniform`] instead.
647 ///
648 /// # Implementation notes
649 ///
650 /// Instead of generating a float in the `[0, 1)` range using [`Standard`], the
651 /// `UniformFloat` implementation converts the output of an PRNG itself. This
652 /// way one or two steps can be optimized out.
653 ///
654 /// The floats are first converted to a value in the `[1, 2)` interval using a
655 /// transmute-based method, and then mapped to the expected range with a
656 /// multiply and addition. Values produced this way have what equals 23 bits of
657 /// random digits for an `f32`, and 52 for an `f64`.
658 ///
659 /// [`new`]: UniformSampler::new
660 /// [`new_inclusive`]: UniformSampler::new_inclusive
661 /// [`Standard`]: crate::distributions::Standard
662 #[derive(Clone, Copy, Debug, PartialEq)]
663 #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
664 pub struct UniformFloat<X> {
665 low: X,
666 scale: X,
667 }
668
669 macro_rules! uniform_float_impl {
670 ($ty:ty, $uty:ident, $f_scalar:ident, $u_scalar:ident, $bits_to_discard:expr) => {
671 impl SampleUniform for $ty {
672 type Sampler = UniformFloat<$ty>;
673 }
674
675 impl UniformSampler for UniformFloat<$ty> {
676 type X = $ty;
677
678 fn new<B1, B2>(low_b: B1, high_b: B2) -> Self
679 where
680 B1: SampleBorrow<Self::X> + Sized,
681 B2: SampleBorrow<Self::X> + Sized,
682 {
683 let low = *low_b.borrow();
684 let high = *high_b.borrow();
685 debug_assert!(
686 low.all_finite(),
687 "Uniform::new called with `low` non-finite."
688 );
689 debug_assert!(
690 high.all_finite(),
691 "Uniform::new called with `high` non-finite."
692 );
693 assert!(low.all_lt(high), "Uniform::new called with `low >= high`");
694 let max_rand = <$ty>::splat(
695 (::core::$u_scalar::MAX >> $bits_to_discard).into_float_with_exponent(0) - 1.0,
696 );
697
698 let mut scale = high - low;
699 assert!(scale.all_finite(), "Uniform::new: range overflow");
700
701 loop {
702 let mask = (scale * max_rand + low).ge_mask(high);
703 if mask.none() {
704 break;
705 }
706 scale = scale.decrease_masked(mask);
707 }
708
709 debug_assert!(<$ty>::splat(0.0).all_le(scale));
710
711 UniformFloat { low, scale }
712 }
713
714 fn new_inclusive<B1, B2>(low_b: B1, high_b: B2) -> Self
715 where
716 B1: SampleBorrow<Self::X> + Sized,
717 B2: SampleBorrow<Self::X> + Sized,
718 {
719 let low = *low_b.borrow();
720 let high = *high_b.borrow();
721 debug_assert!(
722 low.all_finite(),
723 "Uniform::new_inclusive called with `low` non-finite."
724 );
725 debug_assert!(
726 high.all_finite(),
727 "Uniform::new_inclusive called with `high` non-finite."
728 );
729 assert!(
730 low.all_le(high),
731 "Uniform::new_inclusive called with `low > high`"
732 );
733 let max_rand = <$ty>::splat(
734 (::core::$u_scalar::MAX >> $bits_to_discard).into_float_with_exponent(0) - 1.0,
735 );
736
737 let mut scale = (high - low) / max_rand;
738 assert!(scale.all_finite(), "Uniform::new_inclusive: range overflow");
739
740 loop {
741 let mask = (scale * max_rand + low).gt_mask(high);
742 if mask.none() {
743 break;
744 }
745 scale = scale.decrease_masked(mask);
746 }
747
748 debug_assert!(<$ty>::splat(0.0).all_le(scale));
749
750 UniformFloat { low, scale }
751 }
752
753 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
754 // Generate a value in the range [1, 2)
755 let value1_2 = (rng.gen::<$uty>() >> $bits_to_discard).into_float_with_exponent(0);
756
757 // Get a value in the range [0, 1) in order to avoid
758 // overflowing into infinity when multiplying with scale
759 let value0_1 = value1_2 - 1.0;
760
761 // We don't use `f64::mul_add`, because it is not available with
762 // `no_std`. Furthermore, it is slower for some targets (but
763 // faster for others). However, the order of multiplication and
764 // addition is important, because on some platforms (e.g. ARM)
765 // it will be optimized to a single (non-FMA) instruction.
766 value0_1 * self.scale + self.low
767 }
768
769 #[inline]
770 fn sample_single<R: Rng + ?Sized, B1, B2>(low_b: B1, high_b: B2, rng: &mut R) -> Self::X
771 where
772 B1: SampleBorrow<Self::X> + Sized,
773 B2: SampleBorrow<Self::X> + Sized,
774 {
775 let low = *low_b.borrow();
776 let high = *high_b.borrow();
777 debug_assert!(
778 low.all_finite(),
779 "UniformSampler::sample_single called with `low` non-finite."
780 );
781 debug_assert!(
782 high.all_finite(),
783 "UniformSampler::sample_single called with `high` non-finite."
784 );
785 assert!(
786 low.all_lt(high),
787 "UniformSampler::sample_single: low >= high"
788 );
789 let mut scale = high - low;
790 assert!(scale.all_finite(), "UniformSampler::sample_single: range overflow");
791
792 loop {
793 // Generate a value in the range [1, 2)
794 let value1_2 =
795 (rng.gen::<$uty>() >> $bits_to_discard).into_float_with_exponent(0);
796
797 // Get a value in the range [0, 1) in order to avoid
798 // overflowing into infinity when multiplying with scale
799 let value0_1 = value1_2 - 1.0;
800
801 // Doing multiply before addition allows some architectures
802 // to use a single instruction.
803 let res = value0_1 * scale + low;
804
805 debug_assert!(low.all_le(res) || !scale.all_finite());
806 if res.all_lt(high) {
807 return res;
808 }
809
810 // This handles a number of edge cases.
811 // * `low` or `high` is NaN. In this case `scale` and
812 // `res` are going to end up as NaN.
813 // * `low` is negative infinity and `high` is finite.
814 // `scale` is going to be infinite and `res` will be
815 // NaN.
816 // * `high` is positive infinity and `low` is finite.
817 // `scale` is going to be infinite and `res` will
818 // be infinite or NaN (if value0_1 is 0).
819 // * `low` is negative infinity and `high` is positive
820 // infinity. `scale` will be infinite and `res` will
821 // be NaN.
822 // * `low` and `high` are finite, but `high - low`
823 // overflows to infinite. `scale` will be infinite
824 // and `res` will be infinite or NaN (if value0_1 is 0).
825 // So if `high` or `low` are non-finite, we are guaranteed
826 // to fail the `res < high` check above and end up here.
827 //
828 // While we technically should check for non-finite `low`
829 // and `high` before entering the loop, by doing the checks
830 // here instead, we allow the common case to avoid these
831 // checks. But we are still guaranteed that if `low` or
832 // `high` are non-finite we'll end up here and can do the
833 // appropriate checks.
834 //
835 // Likewise `high - low` overflowing to infinity is also
836 // rare, so handle it here after the common case.
837 let mask = !scale.finite_mask();
838 if mask.any() {
839 assert!(
840 low.all_finite() && high.all_finite(),
841 "Uniform::sample_single: low and high must be finite"
842 );
843 scale = scale.decrease_masked(mask);
844 }
845 }
846 }
847 }
848 };
849 }
850
851 uniform_float_impl! { f32, u32, f32, u32, 32 - 23 }
852 uniform_float_impl! { f64, u64, f64, u64, 64 - 52 }
853
854 /// The back-end implementing [`UniformSampler`] for `Duration`.
855 ///
856 /// Unless you are implementing [`UniformSampler`] for your own types, this type
857 /// should not be used directly, use [`Uniform`] instead.
858 #[derive(Clone, Copy, Debug)]
859 #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
860 pub struct UniformDuration {
861 mode: UniformDurationMode,
862 offset: u32,
863 }
864
865 #[derive(Debug, Copy, Clone)]
866 #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
867 enum UniformDurationMode {
868 Small {
869 secs: u64,
870 nanos: Uniform<u32>,
871 },
872 Medium {
873 nanos: Uniform<u64>,
874 },
875 Large {
876 max_secs: u64,
877 max_nanos: u32,
878 secs: Uniform<u64>,
879 },
880 }
881
882 impl SampleUniform for Duration {
883 type Sampler = UniformDuration;
884 }
885
886 impl UniformSampler for UniformDuration {
887 type X = Duration;
888
889 #[inline]
890 fn new<B1, B2>(low_b: B1, high_b: B2) -> Self
891 where
892 B1: SampleBorrow<Self::X> + Sized,
893 B2: SampleBorrow<Self::X> + Sized,
894 {
895 let low = *low_b.borrow();
896 let high = *high_b.borrow();
897 assert!(low < high, "Uniform::new called with `low >= high`");
898 UniformDuration::new_inclusive(low, high - Duration::new(0, 1))
899 }
900
901 #[inline]
902 fn new_inclusive<B1, B2>(low_b: B1, high_b: B2) -> Self
903 where
904 B1: SampleBorrow<Self::X> + Sized,
905 B2: SampleBorrow<Self::X> + Sized,
906 {
907 let low = *low_b.borrow();
908 let high = *high_b.borrow();
909 assert!(
910 low <= high,
911 "Uniform::new_inclusive called with `low > high`"
912 );
913
914 let low_s = low.as_secs();
915 let low_n = low.subsec_nanos();
916 let mut high_s = high.as_secs();
917 let mut high_n = high.subsec_nanos();
918
919 if high_n < low_n {
920 high_s -= 1;
921 high_n += 1_000_000_000;
922 }
923
924 let mode = if low_s == high_s {
925 UniformDurationMode::Small {
926 secs: low_s,
927 nanos: Uniform::new_inclusive(low_n, high_n),
928 }
929 } else {
930 let max = high_s
931 .checked_mul(1_000_000_000)
932 .and_then(|n| n.checked_add(u64::from(high_n)));
933
934 if let Some(higher_bound) = max {
935 let lower_bound = low_s * 1_000_000_000 + u64::from(low_n);
936 UniformDurationMode::Medium {
937 nanos: Uniform::new_inclusive(lower_bound, higher_bound),
938 }
939 } else {
940 // An offset is applied to simplify generation of nanoseconds
941 let max_nanos = high_n - low_n;
942 UniformDurationMode::Large {
943 max_secs: high_s,
944 max_nanos,
945 secs: Uniform::new_inclusive(low_s, high_s),
946 }
947 }
948 };
949 UniformDuration {
950 mode,
951 offset: low_n,
952 }
953 }
954
955 #[inline]
956 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Duration {
957 match self.mode {
958 UniformDurationMode::Small { secs, nanos } => {
959 let n = nanos.sample(rng);
960 Duration::new(secs, n)
961 }
962 UniformDurationMode::Medium { nanos } => {
963 let nanos = nanos.sample(rng);
964 Duration::new(nanos / 1_000_000_000, (nanos % 1_000_000_000) as u32)
965 }
966 UniformDurationMode::Large {
967 max_secs,
968 max_nanos,
969 secs,
970 } => {
971 // constant folding means this is at least as fast as `Rng::sample(Range)`
972 let nano_range = Uniform::new(0, 1_000_000_000);
973 loop {
974 let s = secs.sample(rng);
975 let n = nano_range.sample(rng);
976 if !(s == max_secs && n > max_nanos) {
977 let sum = n + self.offset;
978 break Duration::new(s, sum);
979 }
980 }
981 }
982 }
983 }
984 }
985
986 #[cfg(test)]
987 mod tests {
988 use super::*;
989 use crate::rngs::mock::StepRng;
990
991 #[test]
992 #[cfg(feature = "serde1")]
993 fn test_serialization_uniform_duration() {
994 let distr = UniformDuration::new(Duration::from_secs(10), Duration::from_secs(60));
995 let de_distr: UniformDuration = bincode::deserialize(&bincode::serialize(&distr).unwrap()).unwrap();
996 assert_eq!(
997 distr.offset, de_distr.offset
998 );
999 match (distr.mode, de_distr.mode) {
1000 (UniformDurationMode::Small {secs: a_secs, nanos: a_nanos}, UniformDurationMode::Small {secs, nanos}) => {
1001 assert_eq!(a_secs, secs);
1002
1003 assert_eq!(a_nanos.0.low, nanos.0.low);
1004 assert_eq!(a_nanos.0.range, nanos.0.range);
1005 assert_eq!(a_nanos.0.z, nanos.0.z);
1006 }
1007 (UniformDurationMode::Medium {nanos: a_nanos} , UniformDurationMode::Medium {nanos}) => {
1008 assert_eq!(a_nanos.0.low, nanos.0.low);
1009 assert_eq!(a_nanos.0.range, nanos.0.range);
1010 assert_eq!(a_nanos.0.z, nanos.0.z);
1011 }
1012 (UniformDurationMode::Large {max_secs:a_max_secs, max_nanos:a_max_nanos, secs:a_secs}, UniformDurationMode::Large {max_secs, max_nanos, secs} ) => {
1013 assert_eq!(a_max_secs, max_secs);
1014 assert_eq!(a_max_nanos, max_nanos);
1015
1016 assert_eq!(a_secs.0.low, secs.0.low);
1017 assert_eq!(a_secs.0.range, secs.0.range);
1018 assert_eq!(a_secs.0.z, secs.0.z);
1019 }
1020 _ => panic!("`UniformDurationMode` was not serialized/deserialized correctly")
1021 }
1022 }
1023
1024 #[test]
1025 #[cfg(feature = "serde1")]
1026 fn test_uniform_serialization() {
1027 let unit_box: Uniform<i32> = Uniform::new(-1, 1);
1028 let de_unit_box: Uniform<i32> = bincode::deserialize(&bincode::serialize(&unit_box).unwrap()).unwrap();
1029
1030 assert_eq!(unit_box.0.low, de_unit_box.0.low);
1031 assert_eq!(unit_box.0.range, de_unit_box.0.range);
1032 assert_eq!(unit_box.0.z, de_unit_box.0.z);
1033
1034 let unit_box: Uniform<f32> = Uniform::new(-1., 1.);
1035 let de_unit_box: Uniform<f32> = bincode::deserialize(&bincode::serialize(&unit_box).unwrap()).unwrap();
1036
1037 assert_eq!(unit_box.0.low, de_unit_box.0.low);
1038 assert_eq!(unit_box.0.scale, de_unit_box.0.scale);
1039 }
1040
1041 #[should_panic]
1042 #[test]
1043 fn test_uniform_bad_limits_equal_int() {
1044 Uniform::new(10, 10);
1045 }
1046
1047 #[test]
1048 fn test_uniform_good_limits_equal_int() {
1049 let mut rng = crate::test::rng(804);
1050 let dist = Uniform::new_inclusive(10, 10);
1051 for _ in 0..20 {
1052 assert_eq!(rng.sample(dist), 10);
1053 }
1054 }
1055
1056 #[should_panic]
1057 #[test]
1058 fn test_uniform_bad_limits_flipped_int() {
1059 Uniform::new(10, 5);
1060 }
1061
1062 #[test]
1063 #[cfg_attr(miri, ignore)] // Miri is too slow
1064 fn test_integers() {
1065 use core::{i128, u128};
1066 use core::{i16, i32, i64, i8, isize};
1067 use core::{u16, u32, u64, u8, usize};
1068
1069 let mut rng = crate::test::rng(251);
1070 macro_rules! t {
1071 ($ty:ident, $v:expr, $le:expr, $lt:expr) => {{
1072 for &(low, high) in $v.iter() {
1073 let my_uniform = Uniform::new(low, high);
1074 for _ in 0..1000 {
1075 let v: $ty = rng.sample(my_uniform);
1076 assert!($le(low, v) && $lt(v, high));
1077 }
1078
1079 let my_uniform = Uniform::new_inclusive(low, high);
1080 for _ in 0..1000 {
1081 let v: $ty = rng.sample(my_uniform);
1082 assert!($le(low, v) && $le(v, high));
1083 }
1084
1085 let my_uniform = Uniform::new(&low, high);
1086 for _ in 0..1000 {
1087 let v: $ty = rng.sample(my_uniform);
1088 assert!($le(low, v) && $lt(v, high));
1089 }
1090
1091 let my_uniform = Uniform::new_inclusive(&low, &high);
1092 for _ in 0..1000 {
1093 let v: $ty = rng.sample(my_uniform);
1094 assert!($le(low, v) && $le(v, high));
1095 }
1096
1097 for _ in 0..1000 {
1098 let v = <$ty as SampleUniform>::Sampler::sample_single(low, high, &mut rng);
1099 assert!($le(low, v) && $lt(v, high));
1100 }
1101
1102 for _ in 0..1000 {
1103 let v = <$ty as SampleUniform>::Sampler::sample_single_inclusive(low, high, &mut rng);
1104 assert!($le(low, v) && $le(v, high));
1105 }
1106 }
1107 }};
1108
1109 // scalar bulk
1110 ($($ty:ident),*) => {{
1111 $(t!(
1112 $ty,
1113 [(0, 10), (10, 127), ($ty::MIN, $ty::MAX)],
1114 |x, y| x <= y,
1115 |x, y| x < y
1116 );)*
1117 }};
1118
1119 // simd bulk
1120 ($($ty:ident),* => $scalar:ident) => {{
1121 $(t!(
1122 $ty,
1123 [
1124 ($ty::splat(0), $ty::splat(10)),
1125 ($ty::splat(10), $ty::splat(127)),
1126 ($ty::splat($scalar::MIN), $ty::splat($scalar::MAX)),
1127 ],
1128 |x: $ty, y| x.le(y).all(),
1129 |x: $ty, y| x.lt(y).all()
1130 );)*
1131 }};
1132 }
1133 t!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize, i128, u128);
1134 }
1135
1136 #[test]
1137 #[cfg_attr(miri, ignore)] // Miri is too slow
1138 fn test_char() {
1139 let mut rng = crate::test::rng(891);
1140 let mut max = core::char::from_u32(0).unwrap();
1141 for _ in 0..100 {
1142 let c = rng.gen_range('A'..='Z');
1143 assert!(('A'..='Z').contains(&c));
1144 max = max.max(c);
1145 }
1146 assert_eq!(max, 'Y');
1147 let d = Uniform::new(
1148 core::char::from_u32(0xD7F0).unwrap(),
1149 core::char::from_u32(0xE010).unwrap(),
1150 );
1151 for _ in 0..100 {
1152 let c = d.sample(&mut rng);
1153 assert!((c as u32) < 0xD800 || (c as u32) > 0xDFFF);
1154 }
1155 }
1156
1157 #[test]
1158 #[cfg_attr(miri, ignore)] // Miri is too slow
1159 fn test_floats() {
1160 let mut rng = crate::test::rng(252);
1161 let mut zero_rng = StepRng::new(0, 0);
1162 let mut max_rng = StepRng::new(0xffff_ffff_ffff_ffff, 0);
1163 macro_rules! t {
1164 ($ty:ty, $f_scalar:ident, $bits_shifted:expr) => {{
1165 let v: &[($f_scalar, $f_scalar)] = &[
1166 (0.0, 100.0),
1167 (-1e35, -1e25),
1168 (1e-35, 1e-25),
1169 (-1e35, 1e35),
1170 (<$f_scalar>::from_bits(0), <$f_scalar>::from_bits(3)),
1171 (-<$f_scalar>::from_bits(10), -<$f_scalar>::from_bits(1)),
1172 (-<$f_scalar>::from_bits(5), 0.0),
1173 (-<$f_scalar>::from_bits(7), -0.0),
1174 (0.1 * ::core::$f_scalar::MAX, ::core::$f_scalar::MAX),
1175 (-::core::$f_scalar::MAX * 0.2, ::core::$f_scalar::MAX * 0.7),
1176 ];
1177 for &(low_scalar, high_scalar) in v.iter() {
1178 for lane in 0..<$ty>::lanes() {
1179 let low = <$ty>::splat(0.0 as $f_scalar).replace(lane, low_scalar);
1180 let high = <$ty>::splat(1.0 as $f_scalar).replace(lane, high_scalar);
1181 let my_uniform = Uniform::new(low, high);
1182 let my_incl_uniform = Uniform::new_inclusive(low, high);
1183 for _ in 0..100 {
1184 let v = rng.sample(my_uniform).extract(lane);
1185 assert!(low_scalar <= v && v < high_scalar,"low_scalar <= v && v < high_scalar: low_scalar={}, v={}, high_scalar={}",low_scalar,v,high_scalar);
1186 let v = rng.sample(my_incl_uniform).extract(lane);
1187 assert!(low_scalar <= v && v <= high_scalar);
1188 let v = <$ty as SampleUniform>::Sampler
1189 ::sample_single(low, high, &mut rng).extract(lane);
1190 assert!(low_scalar <= v && v < high_scalar);
1191 }
1192
1193 assert_eq!(
1194 rng.sample(Uniform::new_inclusive(low, low)).extract(lane),
1195 low_scalar
1196 );
1197
1198 assert_eq!(zero_rng.sample(my_uniform).extract(lane), low_scalar);
1199 assert_eq!(zero_rng.sample(my_incl_uniform).extract(lane), low_scalar);
1200 assert_eq!(<$ty as SampleUniform>::Sampler
1201 ::sample_single(low, high, &mut zero_rng)
1202 .extract(lane), low_scalar);
1203 assert!(max_rng.sample(my_uniform).extract(lane) < high_scalar);
1204 assert!(max_rng.sample(my_incl_uniform).extract(lane) <= high_scalar);
1205
1206 // Don't run this test for really tiny differences between high and low
1207 // since for those rounding might result in selecting high for a very
1208 // long time.
1209 if (high_scalar - low_scalar) > 0.0001 {
1210 let mut lowering_max_rng = StepRng::new(
1211 0xffff_ffff_ffff_ffff,
1212 (-1i64 << $bits_shifted) as u64,
1213 );
1214 assert!(
1215 <$ty as SampleUniform>::Sampler
1216 ::sample_single(low, high, &mut lowering_max_rng)
1217 .extract(lane) < high_scalar
1218 );
1219 }
1220 }
1221 }
1222
1223 assert_eq!(
1224 rng.sample(Uniform::new_inclusive(
1225 ::core::$f_scalar::MAX,
1226 ::core::$f_scalar::MAX
1227 )),
1228 ::core::$f_scalar::MAX
1229 );
1230 assert_eq!(
1231 rng.sample(Uniform::new_inclusive(
1232 -::core::$f_scalar::MAX,
1233 -::core::$f_scalar::MAX
1234 )),
1235 -::core::$f_scalar::MAX
1236 );
1237 }};
1238 }
1239
1240 // these tests are known to fail on x87, presumablly because of excess precision.
1241 #[cfg(not(all(target_arch = "x86", not(target_feature = "sse2"))))]
1242 t!(f32, f32, 32 - 23);
1243 #[cfg(not(all(target_arch = "x86", not(target_feature = "sse2"))))]
1244 t!(f64, f64, 64 - 52);
1245 }
1246
1247 #[test]
1248 #[should_panic]
1249 fn test_float_overflow() {
1250 let _ = Uniform::from(::core::f64::MIN..::core::f64::MAX);
1251 }
1252
1253 #[test]
1254 #[should_panic]
1255 fn test_float_overflow_single() {
1256 let mut rng = crate::test::rng(252);
1257 rng.gen_range(::core::f64::MIN..::core::f64::MAX);
1258 }
1259
1260 #[test]
1261 #[cfg(all(
1262 feature = "std",
1263 not(target_arch = "wasm32"),
1264 ))]
1265 fn test_float_assertions() {
1266 use super::SampleUniform;
1267 use std::panic::catch_unwind;
1268 fn range<T: SampleUniform>(low: T, high: T) {
1269 let mut rng = crate::test::rng(253);
1270 T::Sampler::sample_single(low, high, &mut rng);
1271 }
1272
1273 macro_rules! t {
1274 ($ty:ident, $f_scalar:ident) => {{
1275 let v: &[($f_scalar, $f_scalar)] = &[
1276 (::std::$f_scalar::NAN, 0.0),
1277 (1.0, ::std::$f_scalar::NAN),
1278 (::std::$f_scalar::NAN, ::std::$f_scalar::NAN),
1279 (1.0, 0.5),
1280 (::std::$f_scalar::MAX, -::std::$f_scalar::MAX),
1281 (::std::$f_scalar::INFINITY, ::std::$f_scalar::INFINITY),
1282 (
1283 ::std::$f_scalar::NEG_INFINITY,
1284 ::std::$f_scalar::NEG_INFINITY,
1285 ),
1286 (::std::$f_scalar::NEG_INFINITY, 5.0),
1287 (5.0, ::std::$f_scalar::INFINITY),
1288 (::std::$f_scalar::NAN, ::std::$f_scalar::INFINITY),
1289 (::std::$f_scalar::NEG_INFINITY, ::std::$f_scalar::NAN),
1290 (::std::$f_scalar::NEG_INFINITY, ::std::$f_scalar::INFINITY),
1291 ];
1292 for &(low_scalar, high_scalar) in v.iter() {
1293 for lane in 0..<$ty>::lanes() {
1294 let low = <$ty>::splat(0.0 as $f_scalar).replace(lane, low_scalar);
1295 let high = <$ty>::splat(1.0 as $f_scalar).replace(lane, high_scalar);
1296 assert!(catch_unwind(|| range(low, high)).is_err());
1297 assert!(catch_unwind(|| Uniform::new(low, high)).is_err());
1298 assert!(catch_unwind(|| Uniform::new_inclusive(low, high)).is_err());
1299 assert!(catch_unwind(|| range(low, low)).is_err());
1300 assert!(catch_unwind(|| Uniform::new(low, low)).is_err());
1301 }
1302 }
1303 }};
1304 }
1305
1306 t!(f32, f32);
1307 t!(f64, f64);
1308 }
1309
1310
1311 #[test]
1312 #[cfg_attr(miri, ignore)] // Miri is too slow
1313 fn test_durations() {
1314 let mut rng = crate::test::rng(253);
1315
1316 let v = &[
1317 (Duration::new(10, 50000), Duration::new(100, 1234)),
1318 (Duration::new(0, 100), Duration::new(1, 50)),
1319 (
1320 Duration::new(0, 0),
1321 Duration::new(u64::max_value(), 999_999_999),
•1322 ),
1323 ];
1324 for &(low, high) in v.iter() {
1325 let my_uniform = Uniform::new(low, high);
1326 for _ in 0..1000 {
1327 let v = rng.sample(my_uniform);
1328 assert!(low <= v && v < high);
1329 }
1330 }
1331 }
1332
1333 #[test]
1334 fn test_custom_uniform() {
1335 use crate::distributions::uniform::{
1336 SampleBorrow, SampleUniform, UniformFloat, UniformSampler,
1337 };
1338 #[derive(Clone, Copy, PartialEq, PartialOrd)]
1339 struct MyF32 {
1340 x: f32,
1341 }
1342 #[derive(Clone, Copy, Debug)]
1343 struct UniformMyF32(UniformFloat<f32>);
1344 impl UniformSampler for UniformMyF32 {
1345 type X = MyF32;
1346
1347 fn new<B1, B2>(low: B1, high: B2) -> Self
1348 where
1349 B1: SampleBorrow<Self::X> + Sized,
1350 B2: SampleBorrow<Self::X> + Sized,
1351 {
1352 UniformMyF32(UniformFloat::<f32>::new(low.borrow().x, high.borrow().x))
1353 }
1354
1355 fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self
1356 where
1357 B1: SampleBorrow<Self::X> + Sized,
1358 B2: SampleBorrow<Self::X> + Sized,
1359 {
1360 UniformSampler::new(low, high)
•1361 }
1362
1363 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
1364 MyF32 {
1365 x: self.0.sample(rng),
1366 }
1367 }
1368 }
1369 impl SampleUniform for MyF32 {
1370 type Sampler = UniformMyF32;
1371 }
1372
1373 let (low, high) = (MyF32 { x: 17.0f32 }, MyF32 { x: 22.0f32 });
1374 let uniform = Uniform::new(low, high);
1375 let mut rng = crate::test::rng(804);
1376 for _ in 0..100 {
1377 let x: MyF32 = rng.sample(uniform);
1378 assert!(low <= x && x < high);
1379 }
1380 }
1381
1382 #[test]
1383 fn test_uniform_from_std_range() {
1384 let r = Uniform::from(2u32..7);
1385 assert_eq!(r.0.low, 2);
1386 assert_eq!(r.0.range, 5);
1387 let r = Uniform::from(2.0f64..7.0);
1388 assert_eq!(r.0.low, 2.0);
1389 assert_eq!(r.0.scale, 5.0);
1390 }
1391
1392 #[test]
1393 fn test_uniform_from_std_range_inclusive() {
1394 let r = Uniform::from(2u32..=6);
1395 assert_eq!(r.0.low, 2);
1396 assert_eq!(r.0.range, 5);
1397 let r = Uniform::from(2.0f64..=7.0);
1398 assert_eq!(r.0.low, 2.0);
1399 assert!(r.0.scale > 5.0);
1400 assert!(r.0.scale < 5.0 + 1e-14);
1401 }
1402
1403 #[test]
1404 fn value_stability() {
1405 fn test_samples<T: SampleUniform + Copy + core::fmt::Debug + PartialEq>(
1406 lb: T, ub: T, expected_single: &[T], expected_multiple: &[T],
1407 ) where Uniform<T>: Distribution<T> {
1408 let mut rng = crate::test::rng(897);
1409 let mut buf = [lb; 3];
1410
1411 for x in &mut buf {
1412 *x = T::Sampler::sample_single(lb, ub, &mut rng);
1413 }
1414 assert_eq!(&buf, expected_single);
1415
•1416 let distr = Uniform::new(lb, ub);
1417 for x in &mut buf {
1418 *x = rng.sample(&distr);
•1419 }
1420 assert_eq!(&buf, expected_multiple);
1421 }
1422
1423 // We test on a sub-set of types; possibly we should do more.
1424 // TODO: SIMD types
1425
1426 test_samples(11u8, 219, &[40, 98, 153], &[55, 121, 203]);
1427 /*test_samples(11u32, 219, &[17, 66, 214], &[181, 93, 165]);
1428
1429 test_samples(0f32, 1e-2f32, &[0.0003070104, 0.0026630748, 0.00979833], &[
1430 0.008194133,
1431 0.00398172,
1432 0.007428536,
1433 ]);
1434
1435 // this test is known to fail on x87, presumablly because of excess precision.
1436 #[cfg(not(all(target_arch = "x86", not(target_feature = "sse2"))))]
1437 test_samples(
1438 -1e10f64,
1439 1e10f64,
1440 &[-4673848682.871551, 6388267422.932352, 4857075081.198343],
1441 &[1173375212.1808167, 1917642852.109581, 2365076174.3153973],
1442 );
1443
1444 test_samples(
1445 Duration::new(2, 0),
1446 Duration::new(4, 0),
1447 &[
1448 Duration::new(2, 532615131),
1449 Duration::new(3, 638826742),
1450 Duration::new(3, 485707508),
1451 ],
1452 &[
1453 Duration::new(3, 117337521),
1454 Duration::new(3, 191764285),
1455 Duration::new(3, 236507617),
1456 ],
1457 );*/
1458 }
1459
1460 #[test]
1461 fn uniform_distributions_can_be_compared() {
1462 assert_eq!(Uniform::new(1.0, 2.0), Uniform::new(1.0, 2.0));
1463
1464 // To cover UniformInt
1465 assert_eq!(Uniform::new(1 as u32, 2 as u32), Uniform::new(1 as u32, 2 as u32));
1466 }
1467 }