Skip to content

Commit 6d9a44d

Browse files
authored
Merge pull request #456 from jturner314/rand-0.5
Update ndarray-rand to rand 0.5.0
2 parents a4f5ca7 + 5149750 commit 6d9a44d

File tree

5 files changed

+40
-33
lines changed

5 files changed

+40
-33
lines changed

ndarray-rand/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ description = "Constructors for randomized arrays. `rand` integration for `ndarr
1212
keywords = ["multidimensional", "matrix", "rand", "ndarray"]
1313

1414
[dependencies]
15-
rand = "0.4.1"
15+
rand = "0.5.0"
1616
ndarray = { version = "0.11.0", path = ".." }
1717

1818
[package.metadata.release]

ndarray-rand/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ ndarray-rand
44
Recent Changes
55
--------------
66

7+
- 0.8.0 (not yet released)
8+
9+
- Require rand 0.5
10+
711
- 0.7.0
812

913
- Require ndarray 0.11

ndarray-rand/src/lib.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
extern crate rand;
1313
extern 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

1919
use 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).
3239
pub 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)]
110120
pub 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-
}

numeric-tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish = false
77
[dependencies]
88
ndarray = { path = ".." }
99
ndarray-rand = { path = "../ndarray-rand/" }
10-
rand = "0.4.1"
10+
rand = "0.5.0"
1111

1212
[lib]
1313
test = false

numeric-tests/tests/accuracy.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ extern crate ndarray_rand;
55
extern crate rand;
66

77
use ndarray_rand::{RandomExt, F32};
8-
use rand::Rng;
8+
use rand::{FromEntropy, Rng};
9+
use rand::rngs::SmallRng;
910

1011
use ndarray::prelude::*;
1112
use ndarray::{
@@ -74,7 +75,7 @@ fn accurate_eye_f32() {
7475
}
7576
}
7677
// pick a few random sizes
77-
let mut rng = rand::weak_rng();
78+
let mut rng = SmallRng::from_entropy();
7879
for _ in 0..10 {
7980
let i = rng.gen_range(15, 512);
8081
let j = rng.gen_range(15, 512);
@@ -110,7 +111,7 @@ fn accurate_eye_f64() {
110111
}
111112
}
112113
// pick a few random sizes
113-
let mut rng = rand::weak_rng();
114+
let mut rng = SmallRng::from_entropy();
114115
for _ in 0..10 {
115116
let i = rng.gen_range(15, 512);
116117
let j = rng.gen_range(15, 512);
@@ -131,7 +132,7 @@ fn accurate_eye_f64() {
131132
#[test]
132133
fn accurate_mul_f32() {
133134
// pick a few random sizes
134-
let mut rng = rand::weak_rng();
135+
let mut rng = SmallRng::from_entropy();
135136
for i in 0..20 {
136137
let m = rng.gen_range(15, 512);
137138
let k = rng.gen_range(15, 512);
@@ -165,7 +166,7 @@ fn accurate_mul_f32() {
165166
#[test]
166167
fn accurate_mul_f64() {
167168
// pick a few random sizes
168-
let mut rng = rand::weak_rng();
169+
let mut rng = SmallRng::from_entropy();
169170
for i in 0..20 {
170171
let m = rng.gen_range(15, 512);
171172
let k = rng.gen_range(15, 512);
@@ -200,7 +201,7 @@ fn accurate_mul_f64() {
200201
#[test]
201202
fn accurate_mul_with_column_f64() {
202203
// pick a few random sizes
203-
let mut rng = rand::weak_rng();
204+
let mut rng = SmallRng::from_entropy();
204205
for i in 0..10 {
205206
let m = rng.gen_range(1, 350);
206207
let k = rng.gen_range(1, 350);

0 commit comments

Comments
 (0)