12
12
extern crate rand;
13
13
extern crate ndarray;
14
14
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 ;
18
18
19
19
use ndarray:: {
20
20
ArrayBase ,
@@ -28,15 +28,23 @@ use ndarray::ShapeBuilder;
28
28
/// This trait extends ndarray’s `ArrayBase` and can not be implemented
29
29
/// for other types.
30
30
///
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).
32
39
pub trait RandomExt < S , D >
33
40
where S : DataOwned ,
34
41
D : Dimension ,
35
42
{
36
43
/// Create an array with shape `dim` with elements drawn from
37
- /// `distribution` using the default rng .
44
+ /// `distribution` using the default RNG .
38
45
///
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.
40
48
///
41
49
/// ```
42
50
/// extern crate rand;
@@ -55,16 +63,16 @@ pub trait RandomExt<S, D>
55
63
/// // [ 0.0914, 5.5186, 5.8135, 5.2361, 3.1879]]
56
64
/// # }
57
65
fn random < Sh , IdS > ( shape : Sh , distribution : IdS ) -> ArrayBase < S , D >
58
- where IdS : IndependentSample < S :: Elem > ,
66
+ where IdS : Distribution < S :: Elem > ,
59
67
Sh : ShapeBuilder < Dim =D > ;
60
68
61
69
/// Create an array with shape `dim` with elements drawn from
62
70
/// `distribution`, using a specific Rng `rng`.
63
71
///
64
72
/// ***Panics*** if the number of elements overflows usize.
65
73
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 ,
68
76
Sh : ShapeBuilder < Dim =D > ;
69
77
}
70
78
@@ -73,18 +81,20 @@ impl<S, D> RandomExt<S, D> for ArrayBase<S, D>
73
81
D : Dimension ,
74
82
{
75
83
fn random < Sh , IdS > ( shape : Sh , dist : IdS ) -> ArrayBase < S , D >
76
- where IdS : IndependentSample < S :: Elem > ,
84
+ where IdS : Distribution < S :: Elem > ,
77
85
Sh : ShapeBuilder < Dim =D > ,
78
86
{
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)
80
90
}
81
91
82
92
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 ,
85
95
Sh : ShapeBuilder < Dim =D > ,
86
96
{
87
- Self :: from_shape_fn ( shape, |_| dist. ind_sample ( rng) )
97
+ Self :: from_shape_fn ( shape, |_| dist. sample ( rng) )
88
98
}
89
99
}
90
100
@@ -109,18 +119,10 @@ impl<S, D> RandomExt<S, D> for ArrayBase<S, D>
109
119
#[ derive( Copy , Clone , Debug ) ]
110
120
pub struct F32 < S > ( pub S ) ;
111
121
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 >
114
124
{
115
- fn sample < R > ( & mut self , rng : & mut R ) -> f32 where R : Rng {
125
+ fn sample < R : Rng + ? Sized > ( & self , rng : & mut R ) -> f32 {
116
126
self . 0 . sample ( rng) as f32
117
127
}
118
128
}
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