Skip to content

Commit 2d3fb0c

Browse files
authored
Merge pull request #392 from pitdicker/half_open_interval
Suggest `gen_range` as alternative for [0, 1)
2 parents 4da919d + 29329c9 commit 2d3fb0c

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/distributions/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'a, T, D: Distribution<T>> Distribution<T> for &'a D {
260260
/// let mut rng = thread_rng();
261261
/// let erased_rng: &mut RngCore = &mut rng;
262262
/// let val: f32 = erased_rng.sample(Standard);
263-
/// println!("f32 from (0,1): {}", val);
263+
/// println!("f32 from (0, 1): {}", val);
264264
/// ```
265265
///
266266
/// # Open interval for floats
@@ -283,6 +283,17 @@ impl<'a, T, D: Distribution<T>> Distribution<T> for &'a D {
283283
/// than the guarantee some value (`0.0`) is never generated. That makes an open
284284
/// interval a nicer choice.
285285
///
286+
/// Consider using `Rng::gen_range` if you really need a half-open interval (as
287+
/// the ranges use a half-open interval). It has the same performance. Example:
288+
///
289+
/// ```
290+
/// use rand::{thread_rng, Rng};
291+
///
292+
/// let mut rng = thread_rng();
293+
/// let val = rng.gen_range(0.0f32, 1.0);
294+
/// println!("f32 from [0, 1): {}", val);
295+
/// ```
296+
///
286297
/// [`Exp1`]: struct.Exp1.html
287298
/// [`StandardNormal`]: struct.StandardNormal.html
288299
#[derive(Debug)]

src/distributions/range.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ macro_rules! range_int_impl {
290290
}
291291

292292
fn sample_single<R: Rng + ?Sized>(low: Self::X,
293-
high: Self::X,
294-
rng: &mut R) -> Self::X
293+
high: Self::X,
294+
rng: &mut R) -> Self::X
295295
{
296296
let range = (high as $u_large)
297297
.wrapping_sub(low as $u_large);
@@ -473,6 +473,19 @@ macro_rules! range_float_impl {
473473
// it will be optimized to a single (non-FMA) instruction.
474474
value1_2 * self.scale + self.offset
475475
}
476+
477+
fn sample_single<R: Rng + ?Sized>(low: Self::X,
478+
high: Self::X,
479+
rng: &mut R) -> Self::X {
480+
let scale = high - low;
481+
let offset = low - scale;
482+
// Generate a value in the range [1, 2)
483+
let value1_2 = (rng.$next_u() >> $bits_to_discard)
484+
.into_float_with_exponent(0);
485+
// Doing multiply before addition allows some architectures to
486+
// use a single instruction.
487+
value1_2 * scale + offset
488+
}
476489
}
477490
}
478491
}

0 commit comments

Comments
 (0)