Skip to content

Commit

Permalink
Merge pull request #21059 from steveklabnik/gh16072
Browse files Browse the repository at this point in the history
Add note about TLS lookups in random()

Reviewed-by: huonw
  • Loading branch information
bors committed Jan 15, 2015
2 parents 21d553a + 6a7f0a9 commit d2363f6
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/libstd/rand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,13 @@ impl Rng for ThreadRng {
/// `random()` can generate various types of random things, and so may require
/// type hinting to generate the specific type you want.
///
/// This function uses the thread local random number generator. This means
/// that if you're calling `random()` in a loop, caching the generator can
/// increase performance. An example is shown below.
///
/// # Examples
///
/// ```rust
/// ```
/// use std::rand;
///
/// let x = rand::random();
Expand All @@ -389,6 +393,27 @@ impl Rng for ThreadRng {
/// println!("Better lucky than good!");
/// }
/// ```
///
/// Caching the thread local random number generator:
///
/// ```
/// use std::rand;
/// use std::rand::Rng;
///
/// let mut v = vec![1, 2, 3];
///
/// for x in v.iter_mut() {
/// *x = rand::random()
/// }
///
/// // would be faster as
///
/// let mut rng = rand::thread_rng();
///
/// for x in v.iter_mut() {
/// *x = rng.gen();
/// }
/// ```
#[inline]
pub fn random<T: Rand>() -> T {
thread_rng().gen()
Expand Down

0 comments on commit d2363f6

Please sign in to comment.