Skip to content

Commit

Permalink
Replace weak_rng() with new SmallRng
Browse files Browse the repository at this point in the history
`SmallRng` is an opaque wrapper type similar to `StdRng`. See rust-random#289.
  • Loading branch information
vks committed Mar 12, 2018
1 parent 0396bc5 commit 1f0df3c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 24 deletions.
12 changes: 6 additions & 6 deletions benches/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ extern crate rand;

use test::{black_box, Bencher};

use rand::{Rng, weak_rng};
use rand::{SeedableRng, SmallRng, Rng, thread_rng};
use rand::seq::*;

#[bench]
fn misc_shuffle_100(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
let x : &mut [usize] = &mut [1; 100];
b.iter(|| {
rng.shuffle(x);
Expand All @@ -20,7 +20,7 @@ fn misc_shuffle_100(b: &mut Bencher) {

#[bench]
fn misc_sample_iter_10_of_100(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
let x : &[usize] = &[1; 100];
b.iter(|| {
black_box(sample_iter(&mut rng, x, 10).unwrap_or_else(|e| e));
Expand All @@ -29,7 +29,7 @@ fn misc_sample_iter_10_of_100(b: &mut Bencher) {

#[bench]
fn misc_sample_slice_10_of_100(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
let x : &[usize] = &[1; 100];
b.iter(|| {
black_box(sample_slice(&mut rng, x, 10));
Expand All @@ -38,7 +38,7 @@ fn misc_sample_slice_10_of_100(b: &mut Bencher) {

#[bench]
fn misc_sample_slice_ref_10_of_100(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
let x : &[usize] = &[1; 100];
b.iter(|| {
black_box(sample_slice_ref(&mut rng, x, 10));
Expand All @@ -49,7 +49,7 @@ macro_rules! sample_indices {
($name:ident, $amount:expr, $length:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
black_box(sample_indices(&mut rng, $length, $amount));
})
Expand Down
5 changes: 3 additions & 2 deletions src/distributions/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ use distributions::{ziggurat, ziggurat_tables, Distribution};
///
/// # Example
/// ```rust
/// use rand::{weak_rng, Rng};
/// use rand::{SeedableRng, SmallRng, Rng, thread_rng};
/// use rand::distributions::Exp1;
///
/// let val: f64 = weak_rng().sample(Exp1);
/// let val: f64 = SmallRng::from_rng(&mut thread_rng())
/// .unwrap().sample(Exp1);
/// println!("{}", val);
/// ```
#[derive(Clone, Copy, Debug)]
Expand Down
5 changes: 3 additions & 2 deletions src/distributions/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ macro_rules! float_impls {
///
/// # Example
/// ```rust
/// use rand::{weak_rng, Rng};
/// use rand::{SeedableRng, SmallRng, Rng, thread_rng};
/// use rand::distributions::Uniform;
///
/// let val: f32 = weak_rng().sample(Uniform);
/// let val: f32 = SmallRng::from_rng(&mut thread_rng())
/// .unwrap().sample(Uniform);
/// println!("f32 from (0,1): {}", val);
/// ```
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $ty {
Expand Down
5 changes: 3 additions & 2 deletions src/distributions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,11 @@ impl<'a, T, D: Distribution<T>> Distribution<T> for &'a D {
///
/// # Example
/// ```rust
/// use rand::{weak_rng, Rng};
/// use rand::{SeedableRng, SmallRng, Rng, thread_rng};
/// use rand::distributions::Uniform;
///
/// let val: f32 = weak_rng().sample(Uniform);
/// let val: f32 = SmallRng::from_rng(&mut thread_rng())
/// .unwrap().sample(Uniform);
/// println!("f32 from [0,1): {}", val);
/// ```
///
Expand Down
5 changes: 3 additions & 2 deletions src/distributions/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ use distributions::{ziggurat, ziggurat_tables, Distribution, Uniform};
///
/// # Example
/// ```rust
/// use rand::{weak_rng, Rng};
/// use rand::{SeedableRng, SmallRng, Rng, thread_rng};
/// use rand::distributions::StandardNormal;
///
/// let val: f64 = weak_rng().sample(StandardNormal);
/// let val: f64 = SmallRng::from_rng(&mut thread_rng())
/// .unwrap().sample(StandardNormal);
/// println!("{}", val);
/// ```
#[derive(Clone, Copy, Debug)]
Expand Down
58 changes: 48 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,20 +1085,58 @@ impl SeedableRng for StdRng {
}
}

/// Create a weak random number generator with a default algorithm and seed.
/// An RNG recommended when small state, cheap initialization and good
/// performance are required. The PRNG algorithm in `SmallRng` is choosen to be
/// efficient on the current platform, without consideration for cryptography or
/// security. The size of its state is much smaller than for `StdRng`.
///
/// It returns the fastest `Rng` algorithm currently available in Rust without
/// consideration for cryptography or security. If you require a specifically
/// seeded `Rng` for consistency over time you should pick one algorithm and
/// create the `Rng` yourself.
/// Reproducibility of output from this generator is however not required, thus
/// future library versions may use a different internal generator with
/// different output. Further, this generator may not be portable and can
/// produce different output depending on the architecture. If you require
/// reproducible output, use a named RNG, for example `XorShiftRng`.
///
/// This will seed the generator with randomness from thread_rng.
#[cfg(feature="std")]
pub fn weak_rng() -> XorShiftRng {
XorShiftRng::from_rng(&mut thread_rng()).unwrap_or_else(|err|
panic!("weak_rng failed: {:?}", err))
/// The current algorithm used on all platforms is [Xorshift].
///
/// ```
/// use rand::{SeedableRng, SmallRng, thread_rng};
///
/// let _rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
/// ```
///
/// [Xorshift]: struct.XorShiftRng.html
#[derive(Clone, Debug)]
pub struct SmallRng(XorShiftRng);

impl RngCore for SmallRng {
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
}

fn next_u64(&mut self) -> u64 {
self.0.next_u64()
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest);
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}
}

impl SeedableRng for SmallRng {
type Seed = <XorShiftRng as SeedableRng>::Seed;

fn from_seed(seed: Self::Seed) -> Self {
SmallRng(XorShiftRng::from_seed(seed))
}

fn from_rng<R: Rng>(rng: &mut R) -> Result<Self, Error> {
XorShiftRng::from_rng(rng).map(|rng| SmallRng(rng))
}
}

/// DEPRECATED: use `seq::sample_iter` instead.
///
Expand Down

0 comments on commit 1f0df3c

Please sign in to comment.