From 200b0fc2799444205e4dc320abe33544f1ff7de8 Mon Sep 17 00:00:00 2001 From: Tom Prince Date: Thu, 2 Feb 2017 19:56:00 -0700 Subject: [PATCH 1/3] Add a benchmark for `thread_rng`. --- benches/bench.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/benches/bench.rs b/benches/bench.rs index 5fa92bdbea0..c1c2edb7ed2 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -9,7 +9,7 @@ mod distributions; use std::mem::size_of; use test::{black_box, Bencher}; -use rand::{XorShiftRng, StdRng, IsaacRng, Isaac64Rng, Rng}; +use rand::{XorShiftRng, StdRng, IsaacRng, Isaac64Rng, Rng, thread_rng}; use rand::{OsRng, sample, weak_rng}; #[bench] @@ -95,3 +95,12 @@ fn rand_sample_10_of_100(b: &mut Bencher) { sample(&mut rng, x, 10); }) } + +#[bench] +fn rand_thread(b: &mut Bencher) { + let mut rng = thread_rng(); + b.iter(|| for _ in 0..RAND_BENCH_N { + black_box(rng.gen::()); + }); + b.bytes = size_of::() as u64 * RAND_BENCH_N; +} From 1ee0a32edd264d09c4ef7abedf99e2445a7b54f9 Mon Sep 17 00:00:00 2001 From: Tom Prince Date: Sun, 8 Jan 2017 16:23:32 -0700 Subject: [PATCH 2/3] Use OsRng for thread_rng. --- src/lib.rs | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 955a3c8dee2..4b517c2d3af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -862,20 +862,7 @@ pub fn weak_rng() -> XorShiftRng { } } -/// Controls how the thread-local RNG is reseeded. -#[derive(Debug)] -struct ThreadRngReseeder; - -impl reseeding::Reseeder for ThreadRngReseeder { - fn reseed(&mut self, rng: &mut StdRng) { - *rng = match StdRng::new() { - Ok(r) => r, - Err(e) => panic!("could not reseed thread_rng: {}", e) - } - } -} -const THREAD_RNG_RESEED_THRESHOLD: u64 = 32_768; -type ThreadRngInner = reseeding::ReseedingRng; +type ThreadRngInner = os::OsRng; /// The thread-local RNG. #[derive(Clone, Debug)] @@ -889,21 +876,13 @@ pub struct ThreadRng { /// /// The RNG provided will reseed itself from the operating system /// after generating a certain amount of randomness. -/// -/// The internal RNG used is platform and architecture dependent, even -/// if the operating system random number generator is rigged to give -/// the same sequence always. If absolute consistency is required, -/// explicitly select an RNG, e.g. `IsaacRng` or `Isaac64Rng`. pub fn thread_rng() -> ThreadRng { // used to make space in TLS for a random number generator thread_local!(static THREAD_RNG_KEY: Rc> = { - let r = match StdRng::new() { + let rng = match os::OsRng::new() { Ok(r) => r, Err(e) => panic!("could not initialize thread_rng: {}", e) }; - let rng = reseeding::ReseedingRng::new(r, - THREAD_RNG_RESEED_THRESHOLD, - ThreadRngReseeder); Rc::new(RefCell::new(rng)) }); From c9f8b2bf96aed7b13bd322313336865bcbdb85a5 Mon Sep 17 00:00:00 2001 From: Tom Prince Date: Sun, 8 Jan 2017 16:29:44 -0700 Subject: [PATCH 3/3] Update docs. --- src/lib.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4b517c2d3af..3ccb443aea7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,10 +39,9 @@ //! //! There is built-in support for a RNG associated with each thread stored //! in thread-local storage. This RNG can be accessed via `thread_rng`, or -//! used implicitly via `random`. This RNG is normally randomly seeded -//! from an operating-system source of randomness, e.g. `/dev/urandom` on -//! Unix systems, and will automatically reseed itself from this source -//! after generating 32 KiB of random data. +//! used implicitly via `random`. This RNG normally uses +//! an operating-system source of randomness, e.g. `/dev/urandom` on +//! Unix systems. //! //! # Cryptographic security //! @@ -871,11 +870,10 @@ pub struct ThreadRng { } /// Retrieve the lazily-initialized thread-local random number -/// generator, seeded by the system. Intended to be used in method +/// generator. Intended to be used in method /// chaining style, e.g. `thread_rng().gen::()`. /// -/// The RNG provided will reseed itself from the operating system -/// after generating a certain amount of randomness. +/// The RNG provided will use an operating-system source of randomness. pub fn thread_rng() -> ThreadRng { // used to make space in TLS for a random number generator thread_local!(static THREAD_RNG_KEY: Rc> = {