Skip to content

Commit

Permalink
Remove random()
Browse files Browse the repository at this point in the history
See #293.
  • Loading branch information
vks committed Mar 12, 2018
1 parent d953421 commit d606208
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 77 deletions.
18 changes: 6 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@

//! Utilities for random number generation
//!
//! The key functions are `random()` and `Rng::gen()`. These are polymorphic and
//! so can be used to generate any type supporting the [`Uniform`] distribution
//! (i.e. `T` where `Uniform`: `Distribution<T>`). Type inference
//! means that often a simple call to `rand::random()` or `rng.gen()` will
//! suffice, but sometimes an annotation is required, e.g.
//! `rand::random::<f64>()`.
//! The key function is `Rng::gen()`. It is polymorphic and so can be used to
//! generate any type supporting the [`Uniform`] distribution (i.e. `T` where
//! `Uniform`: `Distribution<T>`). Type inference means that often a simple call
//! to `rng.gen()` will suffice, but sometimes an annotation is required, e.g.
//! `rng.gen::<f64>()`.
//!
//! See the `distributions` submodule for sampling random numbers from
//! distributions like normal and exponential.
Expand Down Expand Up @@ -88,11 +87,6 @@
//! }
//! ```
//!
//! ```rust
//! let tuple = rand::random::<(f64, char)>();
//! println!("{:?}", tuple)
//! ```
//!
//! ## Monte Carlo estimation of π
//!
//! For this example, imagine we have a square with sides of length 2 and a unit
Expand Down Expand Up @@ -289,7 +283,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, random};
#[cfg(feature="std")] pub use thread_rng::{ThreadRng, thread_rng};

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

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

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

impl CryptoRng for ThreadRng {}

/// 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
#[inline]
pub fn random<T>() -> T where Uniform: Distribution<T> {
thread_rng().gen()
}

#[cfg(test)]
mod test {
use super::*;
use Rng;

#[test]
#[cfg(feature="std")]
Expand All @@ -163,18 +114,4 @@ mod test {
assert_eq!(v, b);
assert_eq!(r.gen_range(0, 1), 0);
}

#[test]
fn test_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 d606208

Please sign in to comment.