Skip to content

Commit 9c6c115

Browse files
committed
Replace weak_rng() with new SmallRng
`SmallRng` is an opaque wrapper type similar to `StdRng`. See #289.
1 parent d606208 commit 9c6c115

File tree

6 files changed

+66
-24
lines changed

6 files changed

+66
-24
lines changed

benches/misc.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ extern crate rand;
55

66
use test::{black_box, Bencher};
77

8-
use rand::{Rng, weak_rng};
8+
use rand::{SeedableRng, SmallRng, Rng, thread_rng};
99
use rand::seq::*;
1010

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

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

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

3939
#[bench]
4040
fn misc_sample_slice_ref_10_of_100(b: &mut Bencher) {
41-
let mut rng = weak_rng();
41+
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
4242
let x : &[usize] = &[1; 100];
4343
b.iter(|| {
4444
black_box(sample_slice_ref(&mut rng, x, 10));
@@ -49,7 +49,7 @@ macro_rules! sample_indices {
4949
($name:ident, $amount:expr, $length:expr) => {
5050
#[bench]
5151
fn $name(b: &mut Bencher) {
52-
let mut rng = weak_rng();
52+
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
5353
b.iter(|| {
5454
black_box(sample_indices(&mut rng, $length, $amount));
5555
})

src/distributions/exponential.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ use distributions::{ziggurat, ziggurat_tables, Distribution};
3030
///
3131
/// # Example
3232
/// ```rust
33-
/// use rand::{weak_rng, Rng};
33+
/// use rand::{SeedableRng, SmallRng, Rng, thread_rng};
3434
/// use rand::distributions::Exp1;
3535
///
36-
/// let val: f64 = weak_rng().sample(Exp1);
36+
/// let val: f64 = SmallRng::from_rng(&mut thread_rng())
37+
/// .unwrap().sample(Exp1);
3738
/// println!("{}", val);
3839
/// ```
3940
#[derive(Clone, Copy, Debug)]

src/distributions/float.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ macro_rules! float_impls {
4949
///
5050
/// # Example
5151
/// ```rust
52-
/// use rand::{weak_rng, Rng};
52+
/// use rand::{SeedableRng, SmallRng, Rng, thread_rng};
5353
/// use rand::distributions::Uniform;
5454
///
55-
/// let val: f32 = weak_rng().sample(Uniform);
55+
/// let val: f32 = SmallRng::from_rng(&mut thread_rng())
56+
/// .unwrap().sample(Uniform);
5657
/// println!("f32 from (0,1): {}", val);
5758
/// ```
5859
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $ty {

src/distributions/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,11 @@ impl<'a, T, D: Distribution<T>> Distribution<T> for &'a D {
163163
///
164164
/// # Example
165165
/// ```rust
166-
/// use rand::{weak_rng, Rng};
166+
/// use rand::{SeedableRng, SmallRng, Rng, thread_rng};
167167
/// use rand::distributions::Uniform;
168168
///
169-
/// let val: f32 = weak_rng().sample(Uniform);
169+
/// let val: f32 = SmallRng::from_rng(&mut thread_rng())
170+
/// .unwrap().sample(Uniform);
170171
/// println!("f32 from [0,1): {}", val);
171172
/// ```
172173
///

src/distributions/normal.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ use distributions::{ziggurat, ziggurat_tables, Distribution, Uniform};
2828
///
2929
/// # Example
3030
/// ```rust
31-
/// use rand::{weak_rng, Rng};
31+
/// use rand::{SeedableRng, SmallRng, Rng, thread_rng};
3232
/// use rand::distributions::StandardNormal;
3333
///
34-
/// let val: f64 = weak_rng().sample(StandardNormal);
34+
/// let val: f64 = SmallRng::from_rng(&mut thread_rng())
35+
/// .unwrap().sample(StandardNormal);
3536
/// println!("{}", val);
3637
/// ```
3738
#[derive(Clone, Copy, Debug)]

src/lib.rs

+48-10
Original file line numberDiff line numberDiff line change
@@ -1085,20 +1085,58 @@ impl SeedableRng for StdRng {
10851085
}
10861086
}
10871087

1088-
/// Create a weak random number generator with a default algorithm and seed.
1088+
/// An RNG recommended when small state, cheap initialization and good
1089+
/// performance are required. The PRNG algorithm in `SmallRng` is choosen to be
1090+
/// efficient on the current platform, without consideration for cryptography or
1091+
/// security. The size of its state is much smaller than for `StdRng`.
10891092
///
1090-
/// It returns the fastest `Rng` algorithm currently available in Rust without
1091-
/// consideration for cryptography or security. If you require a specifically
1092-
/// seeded `Rng` for consistency over time you should pick one algorithm and
1093-
/// create the `Rng` yourself.
1093+
/// Reproducibility of output from this generator is however not required, thus
1094+
/// future library versions may use a different internal generator with
1095+
/// different output. Further, this generator may not be portable and can
1096+
/// produce different output depending on the architecture. If you require
1097+
/// reproducible output, use a named RNG, for example `XorShiftRng`.
10941098
///
1095-
/// This will seed the generator with randomness from thread_rng.
1096-
#[cfg(feature="std")]
1097-
pub fn weak_rng() -> XorShiftRng {
1098-
XorShiftRng::from_rng(&mut thread_rng()).unwrap_or_else(|err|
1099-
panic!("weak_rng failed: {:?}", err))
1099+
/// The current algorithm used on all platforms is [Xorshift].
1100+
///
1101+
/// ```
1102+
/// use rand::{SeedableRng, SmallRng, thread_rng};
1103+
///
1104+
/// let _rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
1105+
/// ```
1106+
///
1107+
/// [Xorshift]: struct.XorShiftRng.html
1108+
#[derive(Clone, Debug)]
1109+
pub struct SmallRng(XorShiftRng);
1110+
1111+
impl RngCore for SmallRng {
1112+
fn next_u32(&mut self) -> u32 {
1113+
self.0.next_u32()
1114+
}
1115+
1116+
fn next_u64(&mut self) -> u64 {
1117+
self.0.next_u64()
1118+
}
1119+
1120+
fn fill_bytes(&mut self, dest: &mut [u8]) {
1121+
self.0.fill_bytes(dest);
1122+
}
1123+
1124+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
1125+
self.0.try_fill_bytes(dest)
1126+
}
11001127
}
11011128

1129+
impl SeedableRng for SmallRng {
1130+
type Seed = <XorShiftRng as SeedableRng>::Seed;
1131+
1132+
fn from_seed(seed: Self::Seed) -> Self {
1133+
SmallRng(XorShiftRng::from_seed(seed))
1134+
}
1135+
1136+
fn from_rng<R: Rng>(rng: &mut R) -> Result<Self, Error> {
1137+
XorShiftRng::from_rng(rng).map(|rng| SmallRng(rng))
1138+
}
1139+
}
11021140

11031141
/// DEPRECATED: use `seq::sample_iter` instead.
11041142
///

0 commit comments

Comments
 (0)