Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
110 changes: 110 additions & 0 deletions src/distributions/integer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// https://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! The implementations of the `Uniform` distribution for integer types.

use core::mem;

use {Rng};
use distributions::{Distribution, Uniform};

impl Distribution<isize> for Uniform {
#[inline]
fn sample<R: Rng>(&self, rng: &mut R) -> isize {
if mem::size_of::<isize>() == 4 {
rng.gen::<i32>() as isize
} else {
rng.gen::<i64>() as isize
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about platforms with 16 bit pointers? Does Rust support them?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. There was some discussion of 16-bit CPUs here but ultimately because of the extremely limited memory addressing I think most 16-bit code has to be written expressly for that architecture.

}
}
}

impl Distribution<i8> for Uniform {
#[inline]
fn sample<R: Rng>(&self, rng: &mut R) -> i8 {
rng.next_u32() as i8
}
}

impl Distribution<i16> for Uniform {
#[inline]
fn sample<R: Rng>(&self, rng: &mut R) -> i16 {
rng.next_u32() as i16
}
}

impl Distribution<i32> for Uniform {
#[inline]
fn sample<R: Rng>(&self, rng: &mut R) -> i32 {
rng.next_u32() as i32
}
}

impl Distribution<i64> for Uniform {
#[inline]
fn sample<R: Rng>(&self, rng: &mut R) -> i64 {
rng.next_u64() as i64
}
}

#[cfg(feature = "i128_support")]
impl Distribution<i128> for Uniform {
#[inline]
fn sample<R: Rng>(&self, rng: &mut R) -> i128 {
rng.gen::<u128>() as i128
}
}

impl Distribution<usize> for Uniform {
#[inline]
fn sample<R: Rng>(&self, rng: &mut R) -> usize {
if mem::size_of::<usize>() == 4 {
rng.gen::<u32>() as usize
} else {
rng.gen::<u64>() as usize
}
}
}

impl Distribution<u8> for Uniform {
#[inline]
fn sample<R: Rng>(&self, rng: &mut R) -> u8 {
rng.next_u32() as u8
}
}

impl Distribution<u16> for Uniform {
#[inline]
fn sample<R: Rng>(&self, rng: &mut R) -> u16 {
rng.next_u32() as u16
}
}

impl Distribution<u32> for Uniform {
#[inline]
fn sample<R: Rng>(&self, rng: &mut R) -> u32 {
rng.next_u32()
}
}

impl Distribution<u64> for Uniform {
#[inline]
fn sample<R: Rng>(&self, rng: &mut R) -> u64 {
rng.next_u64()
}
}

#[cfg(feature = "i128_support")]
impl Distribution<u128> for Uniform {
#[inline]
fn sample<R: Rng>(&self, rng: &mut R) -> u128 {
((rng.next_u64() as u128) << 64) | (rng.next_u64() as u128)
}
}
16 changes: 15 additions & 1 deletion src/distributions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub mod normal;
pub mod exponential;

mod float;
mod integer;
#[cfg(feature="std")]
mod ziggurat_tables;

Expand Down Expand Up @@ -136,7 +137,18 @@ impl<'a, T, D: Distribution<T>> Distribution<T> for &'a D {
/// For floating-point numbers, this generates values from the half-open range
/// `[0, 1)` (excluding 1). See also [`Open01`] and [`Closed01`] for alternatives.
///
/// TODO: document implementations
/// ## Built-in Implementations
///
/// This crate implements the distribution `Uniform` for various primitive
/// types. Assuming the provided `Rng` is well-behaved, these implementations
/// generate values with the following ranges and distributions:
///
/// * Integers (`i32`, `u32`, `isize`, `usize`, etc.): Uniformly distributed
/// over all values of the type.
/// * Floating point types (`f32` and `f64`): Uniformly distributed in the
/// half-open range `[0, 1)`. (The [`Open01`], [`Closed01`], [`Exp1`], and
/// [`StandardNormal`] distributions produce floating point numbers with
/// alternative ranges or distributions.)
///
/// # Example
/// ```rust
Expand All @@ -149,6 +161,8 @@ impl<'a, T, D: Distribution<T>> Distribution<T> for &'a D {
///
/// [`Open01`]: struct.Open01.html
/// [`Closed01`]: struct.Closed01.html
/// [`Exp1`]: struct.Exp1.html
/// [`StandardNormal`]: struct.StandardNormal.html
#[derive(Debug)]
pub struct Uniform;

Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,6 @@ mod prng;
/// provided `Rng` is well-behaved, these implementations generate values with
/// the following ranges and distributions:
///
/// * Integers (`i32`, `u32`, `isize`, `usize`, etc.): Uniformly distributed
/// over all values of the type.
/// * `char`: Uniformly distributed over all Unicode scalar values, i.e. all
/// code points in the range `0...0x10_FFFF`, except for the range
/// `0xD800...0xDFFF` (the surrogate code points). This includes
Expand Down
97 changes: 1 addition & 96 deletions src/rand_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,105 +10,10 @@

//! The implementations of `Rand` for the built-in types.

use core::{char, mem};
use core::{char};

use {Rand, Rng};

impl Rand for isize {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> isize {
if mem::size_of::<isize>() == 4 {
rng.gen::<i32>() as isize
} else {
rng.gen::<i64>() as isize
}
}
}

impl Rand for i8 {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> i8 {
rng.next_u32() as i8
}
}

impl Rand for i16 {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> i16 {
rng.next_u32() as i16
}
}

impl Rand for i32 {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> i32 {
rng.next_u32() as i32
}
}

impl Rand for i64 {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> i64 {
rng.next_u64() as i64
}
}

#[cfg(feature = "i128_support")]
impl Rand for i128 {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> i128 {
rng.gen::<u128>() as i128
}
}

impl Rand for usize {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> usize {
if mem::size_of::<usize>() == 4 {
rng.gen::<u32>() as usize
} else {
rng.gen::<u64>() as usize
}
}
}

impl Rand for u8 {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> u8 {
rng.next_u32() as u8
}
}

impl Rand for u16 {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> u16 {
rng.next_u32() as u16
}
}

impl Rand for u32 {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> u32 {
rng.next_u32()
}
}

impl Rand for u64 {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> u64 {
rng.next_u64()
}
}

#[cfg(feature = "i128_support")]
impl Rand for u128 {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> u128 {
((rng.next_u64() as u128) << 64) | (rng.next_u64() as u128)
}
}


impl Rand for char {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> char {
Expand Down