@@ -14,6 +14,7 @@ use std::cell::RefCell;
14
14
use std:: rc:: Rc ;
15
15
16
16
use { RngCore , CryptoRng , StdRng , SeedableRng , EntropyRng , Error } ;
17
+ use { Distribution , Rng , Uniform } ;
17
18
use reseeding:: ReseedingRng ;
18
19
19
20
// Number of generated bytes after which to reseed `TreadRng`.
@@ -98,6 +99,57 @@ impl RngCore for ThreadRng {
98
99
99
100
impl CryptoRng for ThreadRng { }
100
101
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
+
101
153
#[ cfg( test) ]
102
154
mod test {
103
155
use Rng ;
@@ -114,4 +166,20 @@ mod test {
114
166
assert_eq ! ( v, b) ;
115
167
assert_eq ! ( r. gen_range( 0 , 1 ) , 0 ) ;
116
168
}
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
+ }
117
185
}
0 commit comments