File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -290,6 +290,19 @@ impl<X: SampleUniform> From<::core::ops::RangeInclusive<X>> for Uniform<X> {
290290 }
291291}
292292
293+ impl < T : SampleUniform > Distribution < T > for :: core:: ops:: Range < T > {
294+ fn sample < R : Rng + ?Sized > ( & self , rng : & mut R ) -> T {
295+ T :: Sampler :: sample_single ( & self . start , & self . end , rng)
296+ }
297+ }
298+
299+ #[ cfg( rustc_1_27) ]
300+ impl < T : SampleUniform > Distribution < T > for :: core:: ops:: RangeInclusive < T > {
301+ fn sample < R : Rng + ?Sized > ( & self , rng : & mut R ) -> T {
302+ Uniform :: new_inclusive ( self . start ( ) , self . end ( ) ) . sample ( rng)
303+ }
304+ }
305+
293306/// Helper trait similar to [`Borrow`] but implemented
294307/// only for SampleUniform and references to SampleUniform in
295308/// order to resolve ambiguity issues.
@@ -1279,6 +1292,17 @@ mod tests {
12791292 assert_eq ! ( r. inner. scale, 5.0 ) ;
12801293 }
12811294
1295+ #[ test]
1296+ fn test_std_range_distribution ( ) {
1297+ let mut rng = :: test:: rng ( 474 ) ;
1298+ for _ in 0 ..100 {
1299+ let x = rng. sample ( 0 ..10 ) ;
1300+ assert ! ( x >= 0 && x < 10 ) ;
1301+ let x = rng. sample ( 0 ..=10 ) ;
1302+ assert ! ( x >= 0 && x <= 10 ) ;
1303+ }
1304+ }
1305+
12821306 #[ cfg( rustc_1_27) ]
12831307 #[ test]
12841308 fn test_uniform_from_std_range_inclusive ( ) {
You can’t perform that action at this time.
0 commit comments