11
11
//! Utilities for random number generation
12
12
//!
13
13
//! The key functions are `random()` and `Rng::gen()`. These are polymorphic and
14
- //! so can be used to generate any type that implements `Rand`. Type inference
14
+ //! so can be used to generate any type supporting the [`Uniform`] distribution
15
+ //! (i.e. `T` where `Uniform`: `Distribution<T>`). Type inference
15
16
//! means that often a simple call to `rand::random()` or `rng.gen()` will
16
17
//! suffice, but sometimes an annotation is required, e.g.
17
18
//! `rand::random::<f64>()`.
236
237
//! keep_wins as f32 / total_keeps as f32);
237
238
//! }
238
239
//! ```
240
+ //!
241
+ //! [`Uniform`]: distributions/struct.Uniform.html
239
242
240
243
#![ doc( html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" ,
241
244
html_favicon_url = "https://www.rust-lang.org/favicon.ico" ,
@@ -286,7 +289,7 @@ use prng::IsaacRng as IsaacWordRng;
286
289
#[ cfg( target_pointer_width = "64" ) ]
287
290
use prng:: Isaac64Rng as IsaacWordRng ;
288
291
289
- use distributions:: { Distribution , Range } ;
292
+ use distributions:: { Distribution , Uniform , Range } ;
290
293
use distributions:: range:: SampleRange ;
291
294
#[ cfg( feature="std" ) ] use reseeding:: ReseedingRng ;
292
295
@@ -321,6 +324,7 @@ mod prng;
321
324
/// convenience and backwards-compatibility.
322
325
///
323
326
/// [`Uniform`]: distributions/struct.Uniform.html
327
+ #[ deprecated( since="0.5.0" , note="replaced by distributions::Uniform" ) ]
324
328
pub trait Rand : Sized {
325
329
/// Generates a random instance of this type using the specified source of
326
330
/// randomness.
@@ -519,7 +523,9 @@ pub trait Rng: RngCore + Sized {
519
523
distr. sample ( self )
520
524
}
521
525
522
- /// Return a random value of a `Rand` type.
526
+ /// Return a random value supporting the [`Uniform`] distribution.
527
+ ///
528
+ /// [`Uniform`]: struct.Uniform.html
523
529
///
524
530
/// # Example
525
531
///
@@ -532,8 +538,8 @@ pub trait Rng: RngCore + Sized {
532
538
/// println!("{:?}", rng.gen::<(f64, bool)>());
533
539
/// ```
534
540
#[ inline( always) ]
535
- fn gen < T : Rand > ( & mut self ) -> T {
536
- Rand :: rand ( self )
541
+ fn gen < T > ( & mut self ) -> T where Uniform : Distribution < T > {
542
+ Uniform . sample ( self )
537
543
}
538
544
539
545
/// Return an iterator that will yield an infinite number of randomly
@@ -550,7 +556,7 @@ pub trait Rng: RngCore + Sized {
550
556
/// println!("{:?}", rng.gen_iter::<(f64, bool)>().take(5)
551
557
/// .collect::<Vec<(f64, bool)>>());
552
558
/// ```
553
- fn gen_iter < ' a , T : Rand > ( & ' a mut self ) -> Generator < ' a , T , Self > {
559
+ fn gen_iter < ' a , T > ( & ' a mut self ) -> Generator < ' a , T , Self > where Uniform : Distribution < T > {
554
560
Generator { rng : self , _marker : marker:: PhantomData }
555
561
}
556
562
@@ -752,7 +758,7 @@ pub struct Generator<'a, T, R:'a> {
752
758
_marker : marker:: PhantomData < fn ( ) -> T > ,
753
759
}
754
760
755
- impl < ' a , T : Rand , R : RngCore > Iterator for Generator < ' a , T , R > {
761
+ impl < ' a , T , R : RngCore > Iterator for Generator < ' a , T , R > where Uniform : Distribution < T > {
756
762
type Item = T ;
757
763
758
764
fn next ( & mut self ) -> Option < T > {
@@ -1203,7 +1209,7 @@ impl RngCore for EntropyRng {
1203
1209
/// [`Rand`]: trait.Rand.html
1204
1210
#[ cfg( feature="std" ) ]
1205
1211
#[ inline]
1206
- pub fn random < T : Rand > ( ) -> T {
1212
+ pub fn random < T > ( ) -> T where Uniform : Distribution < T > {
1207
1213
thread_rng ( ) . gen ( )
1208
1214
}
1209
1215
0 commit comments