Skip to content

Commit

Permalink
Readd random() with a deprecation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
vks committed Mar 12, 2018
1 parent 9c6c115 commit 9c49275
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ pub use error::{ErrorKind, Error};
// convenience and derived rngs
#[cfg(feature="std")] pub use entropy_rng::EntropyRng;
#[cfg(feature="std")] pub use thread_rng::{ThreadRng, thread_rng};
#[cfg(feature="std")] #[allow(deprecated)] pub use thread_rng::random;

use distributions::{Distribution, Uniform, Range};
use distributions::range::SampleRange;
Expand Down
68 changes: 68 additions & 0 deletions src/thread_rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::cell::RefCell;
use std::rc::Rc;

use {RngCore, CryptoRng, StdRng, SeedableRng, EntropyRng, Error};
use {Distribution, Rng, Uniform};
use reseeding::ReseedingRng;

// Number of generated bytes after which to reseed `TreadRng`.
Expand Down Expand Up @@ -98,6 +99,57 @@ impl RngCore for ThreadRng {

impl CryptoRng for ThreadRng {}

/// DEPRECATED: use `thread_rng().gen()` instead.
///
/// Generates a random value using the thread-local random number generator.
///
/// This is simply a shortcut for `thread_rng().gen()`. See [`thread_rng`] for
/// documentation of the entropy source and [`Rand`] for documentation of
/// distributions and type-specific generation.
///
/// # Examples
///
/// ```
/// let x = rand::random::<u8>();
/// println!("{}", x);
///
/// let y = rand::random::<f64>();
/// println!("{}", y);
///
/// if rand::random() { // generates a boolean
/// println!("Better lucky than good!");
/// }
/// ```
///
/// If you're calling `random()` in a loop, caching the generator as in the
/// following example can increase performance.
///
/// ```
/// use rand::Rng;
///
/// let mut v = vec![1, 2, 3];
///
/// for x in v.iter_mut() {
/// *x = rand::random()
/// }
///
/// // can be made faster by caching thread_rng
///
/// let mut rng = rand::thread_rng();
///
/// for x in v.iter_mut() {
/// *x = rng.gen();
/// }
/// ```
///
/// [`thread_rng`]: fn.thread_rng.html
/// [`Rand`]: trait.Rand.html
#[deprecated(since="0.5.0", note="removed in favor of thread_rng().gen()")]
#[inline]
pub fn random<T>() -> T where Uniform: Distribution<T> {
thread_rng().gen()
}

#[cfg(test)]
mod test {
use Rng;
Expand All @@ -114,4 +166,20 @@ mod test {
assert_eq!(v, b);
assert_eq!(r.gen_range(0, 1), 0);
}

#[test]
#[allow(deprecated)]
fn test_random() {
use super::random;
// not sure how to test this aside from just getting some values
let _n : usize = random();
let _f : f32 = random();
let _o : Option<Option<i8>> = random();
let _many : ((),
(usize,
isize,
Option<(u32, (bool,))>),
(u8, i8, u16, i16, u32, i32, u64, i64),
(f32, (f64, (f64,)))) = random();
}
}

0 comments on commit 9c49275

Please sign in to comment.