Skip to content

Commit 9c49275

Browse files
committedMar 12, 2018
Readd random() with a deprecation warning
1 parent 9c6c115 commit 9c49275

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
 

‎src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ pub use error::{ErrorKind, Error};
284284
// convenience and derived rngs
285285
#[cfg(feature="std")] pub use entropy_rng::EntropyRng;
286286
#[cfg(feature="std")] pub use thread_rng::{ThreadRng, thread_rng};
287+
#[cfg(feature="std")] #[allow(deprecated)] pub use thread_rng::random;
287288

288289
use distributions::{Distribution, Uniform, Range};
289290
use distributions::range::SampleRange;

‎src/thread_rng.rs

+68
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::cell::RefCell;
1414
use std::rc::Rc;
1515

1616
use {RngCore, CryptoRng, StdRng, SeedableRng, EntropyRng, Error};
17+
use {Distribution, Rng, Uniform};
1718
use reseeding::ReseedingRng;
1819

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

99100
impl CryptoRng for ThreadRng {}
100101

102+
/// DEPRECATED: use `thread_rng().gen()` instead.
103+
///
104+
/// Generates a random value using the thread-local random number generator.
105+
///
106+
/// This is simply a shortcut for `thread_rng().gen()`. See [`thread_rng`] for
107+
/// documentation of the entropy source and [`Rand`] for documentation of
108+
/// distributions and type-specific generation.
109+
///
110+
/// # Examples
111+
///
112+
/// ```
113+
/// let x = rand::random::<u8>();
114+
/// println!("{}", x);
115+
///
116+
/// let y = rand::random::<f64>();
117+
/// println!("{}", y);
118+
///
119+
/// if rand::random() { // generates a boolean
120+
/// println!("Better lucky than good!");
121+
/// }
122+
/// ```
123+
///
124+
/// If you're calling `random()` in a loop, caching the generator as in the
125+
/// following example can increase performance.
126+
///
127+
/// ```
128+
/// use rand::Rng;
129+
///
130+
/// let mut v = vec![1, 2, 3];
131+
///
132+
/// for x in v.iter_mut() {
133+
/// *x = rand::random()
134+
/// }
135+
///
136+
/// // can be made faster by caching thread_rng
137+
///
138+
/// let mut rng = rand::thread_rng();
139+
///
140+
/// for x in v.iter_mut() {
141+
/// *x = rng.gen();
142+
/// }
143+
/// ```
144+
///
145+
/// [`thread_rng`]: fn.thread_rng.html
146+
/// [`Rand`]: trait.Rand.html
147+
#[deprecated(since="0.5.0", note="removed in favor of thread_rng().gen()")]
148+
#[inline]
149+
pub fn random<T>() -> T where Uniform: Distribution<T> {
150+
thread_rng().gen()
151+
}
152+
101153
#[cfg(test)]
102154
mod test {
103155
use Rng;
@@ -114,4 +166,20 @@ mod test {
114166
assert_eq!(v, b);
115167
assert_eq!(r.gen_range(0, 1), 0);
116168
}
169+
170+
#[test]
171+
#[allow(deprecated)]
172+
fn test_random() {
173+
use super::random;
174+
// not sure how to test this aside from just getting some values
175+
let _n : usize = random();
176+
let _f : f32 = random();
177+
let _o : Option<Option<i8>> = random();
178+
let _many : ((),
179+
(usize,
180+
isize,
181+
Option<(u32, (bool,))>),
182+
(u8, i8, u16, i16, u32, i32, u64, i64),
183+
(f32, (f64, (f64,)))) = random();
184+
}
117185
}

0 commit comments

Comments
 (0)