1212extern crate rand;
1313extern crate ndarray;
1414
15- use rand:: Rng ;
16- use rand:: distributions:: Sample ;
17- use rand:: distributions :: IndependentSample ;
15+ use rand:: { thread_rng , Rng , SeedableRng } ;
16+ use rand:: distributions:: Distribution ;
17+ use rand:: rngs :: SmallRng ;
1818
1919use ndarray:: {
2020 ArrayBase ,
@@ -28,15 +28,23 @@ use ndarray::ShapeBuilder;
2828/// This trait extends ndarray’s `ArrayBase` and can not be implemented
2929/// for other types.
3030///
31- /// The default Rng is a fast automatically seeded rng (currently `rand::weak_rng`).
31+ /// The default RNG is a fast automatically seeded rng (currently
32+ /// [`rand::rngs::SmallRng`](https://docs.rs/rand/0.5/rand/rngs/struct.SmallRng.html)
33+ /// seeded from [`rand::thread_rng`](https://docs.rs/rand/0.5/rand/fn.thread_rng.html)).
34+ ///
35+ /// Note that `SmallRng` is cheap to initialize and fast, but it may generate
36+ /// low-quality random numbers, and reproducibility is not guaranteed. See its
37+ /// documentation for information. You can select a different RNG with
38+ /// [`.random_using()`](#tymethod.random_using).
3239pub trait RandomExt < S , D >
3340 where S : DataOwned ,
3441 D : Dimension ,
3542{
3643 /// Create an array with shape `dim` with elements drawn from
37- /// `distribution` using the default rng .
44+ /// `distribution` using the default RNG .
3845 ///
39- /// ***Panics*** if the number of elements overflows usize.
46+ /// ***Panics*** if creation of the RNG fails or if the number of elements
47+ /// overflows usize.
4048 ///
4149 /// ```
4250 /// extern crate rand;
@@ -55,16 +63,16 @@ pub trait RandomExt<S, D>
5563 /// // [ 0.0914, 5.5186, 5.8135, 5.2361, 3.1879]]
5664 /// # }
5765 fn random < Sh , IdS > ( shape : Sh , distribution : IdS ) -> ArrayBase < S , D >
58- where IdS : IndependentSample < S :: Elem > ,
66+ where IdS : Distribution < S :: Elem > ,
5967 Sh : ShapeBuilder < Dim =D > ;
6068
6169 /// Create an array with shape `dim` with elements drawn from
6270 /// `distribution`, using a specific Rng `rng`.
6371 ///
6472 /// ***Panics*** if the number of elements overflows usize.
6573 fn random_using < Sh , IdS , R > ( shape : Sh , distribution : IdS , rng : & mut R ) -> ArrayBase < S , D >
66- where IdS : IndependentSample < S :: Elem > ,
67- R : Rng ,
74+ where IdS : Distribution < S :: Elem > ,
75+ R : Rng + ? Sized ,
6876 Sh : ShapeBuilder < Dim =D > ;
6977}
7078
@@ -73,18 +81,20 @@ impl<S, D> RandomExt<S, D> for ArrayBase<S, D>
7381 D : Dimension ,
7482{
7583 fn random < Sh , IdS > ( shape : Sh , dist : IdS ) -> ArrayBase < S , D >
76- where IdS : IndependentSample < S :: Elem > ,
84+ where IdS : Distribution < S :: Elem > ,
7785 Sh : ShapeBuilder < Dim =D > ,
7886 {
79- Self :: random_using ( shape, dist, & mut rand:: weak_rng ( ) )
87+ let mut rng =
88+ SmallRng :: from_rng ( thread_rng ( ) ) . expect ( "create SmallRng from thread_rng failed" ) ;
89+ Self :: random_using ( shape, dist, & mut rng)
8090 }
8191
8292 fn random_using < Sh , IdS , R > ( shape : Sh , dist : IdS , rng : & mut R ) -> ArrayBase < S , D >
83- where IdS : IndependentSample < S :: Elem > ,
84- R : Rng ,
93+ where IdS : Distribution < S :: Elem > ,
94+ R : Rng + ? Sized ,
8595 Sh : ShapeBuilder < Dim =D > ,
8696 {
87- Self :: from_shape_fn ( shape, |_| dist. ind_sample ( rng) )
97+ Self :: from_shape_fn ( shape, |_| dist. sample ( rng) )
8898 }
8999}
90100
@@ -109,18 +119,10 @@ impl<S, D> RandomExt<S, D> for ArrayBase<S, D>
109119#[ derive( Copy , Clone , Debug ) ]
110120pub struct F32 < S > ( pub S ) ;
111121
112- impl < S > Sample < f32 > for F32 < S >
113- where S : Sample < f64 >
122+ impl < S > Distribution < f32 > for F32 < S >
123+ where S : Distribution < f64 >
114124{
115- fn sample < R > ( & mut self , rng : & mut R ) -> f32 where R : Rng {
125+ fn sample < R : Rng + ? Sized > ( & self , rng : & mut R ) -> f32 {
116126 self . 0 . sample ( rng) as f32
117127 }
118128}
119-
120- impl < S > IndependentSample < f32 > for F32 < S >
121- where S : IndependentSample < f64 >
122- {
123- fn ind_sample < R > ( & self , rng : & mut R ) -> f32 where R : Rng {
124- self . 0 . ind_sample ( rng) as f32
125- }
126- }
0 commit comments