Skip to content

Commit 9fc4110

Browse files
authored
Bump rand to 0.9.0 and rand_distr to 0.5.0 (#1486)
1 parent ee6c45e commit 9fc4110

File tree

6 files changed

+40
-39
lines changed

6 files changed

+40
-39
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ num-traits = { version = "0.2", default-features = false }
9696
num-complex = { version = "0.4", default-features = false }
9797
approx = { version = "0.5", default-features = false }
9898
quickcheck = { version = "1.0", default-features = false }
99-
rand = { version = "0.8.0", features = ["small_rng"] }
100-
rand_distr = { version = "0.4.0" }
99+
rand = { version = "0.9.0", features = ["small_rng"] }
100+
rand_distr = { version = "0.5.0" }
101101
itertools = { version = "0.13.0", default-features = false, features = ["use_std"] }
102102
cblas-sys = { version = "0.1.4", default-features = false }
103103

crates/numeric-tests/tests/accuracy.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ where
8686
#[test]
8787
fn accurate_eye_f32()
8888
{
89-
let rng = &mut SmallRng::from_entropy();
89+
let rng = &mut SmallRng::from_os_rng();
9090
for i in 0..20 {
9191
let eye = Array::eye(i);
9292
for j in 0..20 {
@@ -99,8 +99,8 @@ fn accurate_eye_f32()
9999
}
100100
// pick a few random sizes
101101
for _ in 0..10 {
102-
let i = rng.gen_range(15..512);
103-
let j = rng.gen_range(15..512);
102+
let i = rng.random_range(15..512);
103+
let j = rng.random_range(15..512);
104104
println!("Testing size {} by {}", i, j);
105105
let a = gen::<f32, _>(Ix2(i, j), rng);
106106
let eye = Array::eye(i);
@@ -114,7 +114,7 @@ fn accurate_eye_f32()
114114
#[test]
115115
fn accurate_eye_f64()
116116
{
117-
let rng = &mut SmallRng::from_entropy();
117+
let rng = &mut SmallRng::from_os_rng();
118118
let abs_tol = 1e-15;
119119
for i in 0..20 {
120120
let eye = Array::eye(i);
@@ -128,8 +128,8 @@ fn accurate_eye_f64()
128128
}
129129
// pick a few random sizes
130130
for _ in 0..10 {
131-
let i = rng.gen_range(15..512);
132-
let j = rng.gen_range(15..512);
131+
let i = rng.random_range(15..512);
132+
let j = rng.random_range(15..512);
133133
println!("Testing size {} by {}", i, j);
134134
let a = gen::<f64, _>(Ix2(i, j), rng);
135135
let eye = Array::eye(i);
@@ -172,9 +172,9 @@ fn random_matrix_mul<A>(
172172
) -> (Array2<A>, Array2<A>)
173173
where A: LinalgScalar
174174
{
175-
let m = rng.gen_range(15..128);
176-
let k = rng.gen_range(15..128);
177-
let n = rng.gen_range(15..512);
175+
let m = rng.random_range(15..128);
176+
let k = rng.random_range(15..128);
177+
let n = rng.random_range(15..512);
178178
let a = generator(Ix2(m, k), rng);
179179
let b = generator(Ix2(n, k), rng);
180180
let c = if use_general {
@@ -209,7 +209,7 @@ where
209209
A: fmt::Debug,
210210
{
211211
// pick a few random sizes
212-
let mut rng = SmallRng::from_entropy();
212+
let mut rng = SmallRng::from_os_rng();
213213
for i in 0..20 {
214214
let (c, reference) = random_matrix_mul(&mut rng, i > 10, use_general, gen::<A, _>);
215215

@@ -241,7 +241,7 @@ where
241241
A: fmt::Debug,
242242
{
243243
// pick a few random sizes
244-
let mut rng = SmallRng::from_entropy();
244+
let mut rng = SmallRng::from_os_rng();
245245
for i in 0..20 {
246246
let (c, reference) = random_matrix_mul(&mut rng, i > 10, true, gen_complex::<A, _>);
247247

@@ -259,10 +259,10 @@ where
259259
fn accurate_mul_with_column_f64()
260260
{
261261
// pick a few random sizes
262-
let rng = &mut SmallRng::from_entropy();
262+
let rng = &mut SmallRng::from_os_rng();
263263
for i in 0..10 {
264-
let m = rng.gen_range(1..128);
265-
let k = rng.gen_range(1..350);
264+
let m = rng.random_range(1..128);
265+
let k = rng.random_range(1..350);
266266
let a = gen::<f64, _>(Ix2(m, k), rng);
267267
let b_owner = gen::<f64, _>(Ix2(k, k), rng);
268268
let b_row_col;

ndarray-rand/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ rand_distr = { workspace = true }
2121
quickcheck = { workspace = true, optional = true }
2222

2323
[dev-dependencies]
24-
rand_isaac = "0.3.0"
24+
rand_isaac = "0.4.0"
2525
quickcheck = { workspace = true }
2626

2727
[package.metadata.release]

ndarray-rand/benches/bench.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use test::Bencher;
1313
fn uniform_f32(b: &mut Bencher)
1414
{
1515
let m = 100;
16-
b.iter(|| Array::random((m, m), Uniform::new(-1f32, 1.)));
16+
b.iter(|| Array::random((m, m), Uniform::new(-1f32, 1.).unwrap()));
1717
}
1818

1919
#[bench]

ndarray-rand/src/lib.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
//! that the items are not compatible (e.g. that a type doesn't implement a
3030
//! necessary trait).
3131
32-
use crate::rand::distributions::{Distribution, Uniform};
32+
use crate::rand::distr::{Distribution, Uniform};
3333
use crate::rand::rngs::SmallRng;
3434
use crate::rand::seq::index;
35-
use crate::rand::{thread_rng, Rng, SeedableRng};
35+
use crate::rand::{rng, Rng, SeedableRng};
3636

3737
use ndarray::{Array, Axis, RemoveAxis, ShapeBuilder};
3838
use ndarray::{ArrayBase, Data, DataOwned, Dimension, RawData};
@@ -71,16 +71,16 @@ where
7171
/// Create an array with shape `dim` with elements drawn from
7272
/// `distribution` using the default RNG.
7373
///
74-
/// ***Panics*** if creation of the RNG fails or if the number of elements
75-
/// overflows usize.
74+
/// ***Panics*** if creation of the RNG fails, the number of elements
75+
/// overflows usize, or the axis has zero length.
7676
///
7777
/// ```
7878
/// use ndarray::Array;
7979
/// use ndarray_rand::RandomExt;
8080
/// use ndarray_rand::rand_distr::Uniform;
8181
///
8282
/// # fn main() {
83-
/// let a = Array::random((2, 5), Uniform::new(0., 10.));
83+
/// let a = Array::random((2, 5), Uniform::new(0., 10.).unwrap());
8484
/// println!("{:8.4}", a);
8585
/// // Example Output:
8686
/// // [[ 8.6900, 6.9824, 3.8922, 6.5861, 2.4890],
@@ -95,7 +95,8 @@ where
9595
/// Create an array with shape `dim` with elements drawn from
9696
/// `distribution`, using a specific Rng `rng`.
9797
///
98-
/// ***Panics*** if the number of elements overflows usize.
98+
/// ***Panics*** if the number of elements overflows usize
99+
/// or the axis has zero length.
99100
///
100101
/// ```
101102
/// use ndarray::Array;
@@ -110,7 +111,7 @@ where
110111
/// let mut rng = Isaac64Rng::seed_from_u64(seed);
111112
///
112113
/// // Generate a random array using `rng`
113-
/// let a = Array::random_using((2, 5), Uniform::new(0., 10.), &mut rng);
114+
/// let a = Array::random_using((2, 5), Uniform::new(0., 10.).unwrap(), &mut rng);
114115
/// println!("{:8.4}", a);
115116
/// // Example Output:
116117
/// // [[ 8.6900, 6.9824, 3.8922, 6.5861, 2.4890],
@@ -270,7 +271,7 @@ where
270271
{
271272
let indices: Vec<_> = match strategy {
272273
SamplingStrategy::WithReplacement => {
273-
let distribution = Uniform::from(0..self.len_of(axis));
274+
let distribution = Uniform::new(0, self.len_of(axis)).unwrap();
274275
(0..n_samples).map(|_| distribution.sample(rng)).collect()
275276
}
276277
SamplingStrategy::WithoutReplacement => index::sample(rng, self.len_of(axis), n_samples).into_vec(),
@@ -308,5 +309,5 @@ impl Arbitrary for SamplingStrategy
308309

309310
fn get_rng() -> SmallRng
310311
{
311-
SmallRng::from_rng(thread_rng()).expect("create SmallRng from thread_rng failed")
312+
SmallRng::from_rng(&mut rng())
312313
}

ndarray-rand/tests/tests.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ndarray::{Array, Array2, ArrayView1, Axis};
22
#[cfg(feature = "quickcheck")]
3-
use ndarray_rand::rand::{distributions::Distribution, thread_rng};
3+
use ndarray_rand::rand::{distr::Distribution, rng};
44

55
use ndarray::ShapeBuilder;
66
use ndarray_rand::rand_distr::Uniform;
@@ -13,7 +13,7 @@ fn test_dim()
1313
let (mm, nn) = (5, 5);
1414
for m in 0..mm {
1515
for n in 0..nn {
16-
let a = Array::random((m, n), Uniform::new(0., 2.));
16+
let a = Array::random((m, n), Uniform::new(0., 2.).unwrap());
1717
assert_eq!(a.shape(), &[m, n]);
1818
assert!(a.iter().all(|x| *x < 2.));
1919
assert!(a.iter().all(|x| *x >= 0.));
@@ -28,7 +28,7 @@ fn test_dim_f()
2828
let (mm, nn) = (5, 5);
2929
for m in 0..mm {
3030
for n in 0..nn {
31-
let a = Array::random((m, n).f(), Uniform::new(0., 2.));
31+
let a = Array::random((m, n).f(), Uniform::new(0., 2.).unwrap());
3232
assert_eq!(a.shape(), &[m, n]);
3333
assert!(a.iter().all(|x| *x < 2.));
3434
assert!(a.iter().all(|x| *x >= 0.));
@@ -41,7 +41,7 @@ fn test_dim_f()
4141
fn sample_axis_on_view()
4242
{
4343
let m = 5;
44-
let a = Array::random((m, 4), Uniform::new(0., 2.));
44+
let a = Array::random((m, 4), Uniform::new(0., 2.).unwrap());
4545
let _samples = a
4646
.view()
4747
.sample_axis(Axis(0), m, SamplingStrategy::WithoutReplacement);
@@ -52,15 +52,15 @@ fn sample_axis_on_view()
5252
fn oversampling_without_replacement_should_panic()
5353
{
5454
let m = 5;
55-
let a = Array::random((m, 4), Uniform::new(0., 2.));
55+
let a = Array::random((m, 4), Uniform::new(0., 2.).unwrap());
5656
let _samples = a.sample_axis(Axis(0), m + 1, SamplingStrategy::WithoutReplacement);
5757
}
5858

5959
quickcheck! {
6060
#[cfg_attr(miri, ignore)] // Takes an insufferably long time
6161
fn oversampling_with_replacement_is_fine(m: u8, n: u8) -> TestResult {
6262
let (m, n) = (m as usize, n as usize);
63-
let a = Array::random((m, n), Uniform::new(0., 2.));
63+
let a = Array::random((m, n), Uniform::new(0., 2.).unwrap());
6464
// Higher than the length of both axes
6565
let n_samples = m + n + 1;
6666

@@ -90,12 +90,12 @@ quickcheck! {
9090
#[cfg_attr(miri, ignore)] // This takes *forever* with Miri
9191
fn sampling_behaves_as_expected(m: u8, n: u8, strategy: SamplingStrategy) -> TestResult {
9292
let (m, n) = (m as usize, n as usize);
93-
let a = Array::random((m, n), Uniform::new(0., 2.));
94-
let mut rng = &mut thread_rng();
93+
let a = Array::random((m, n), Uniform::new(0., 2.).unwrap());
94+
let mut rng = &mut rng();
9595

9696
// We don't want to deal with sampling from 0-length axes in this test
9797
if m != 0 {
98-
let n_row_samples = Uniform::from(1..m+1).sample(&mut rng);
98+
let n_row_samples = Uniform::new(1, m+1).unwrap().sample(&mut rng);
9999
if !sampling_works(&a, strategy.clone(), Axis(0), n_row_samples) {
100100
return TestResult::failed();
101101
}
@@ -105,7 +105,7 @@ quickcheck! {
105105

106106
// We don't want to deal with sampling from 0-length axes in this test
107107
if n != 0 {
108-
let n_col_samples = Uniform::from(1..n+1).sample(&mut rng);
108+
let n_col_samples = Uniform::new(1, n+1).unwrap().sample(&mut rng);
109109
if !sampling_works(&a, strategy, Axis(1), n_col_samples) {
110110
return TestResult::failed();
111111
}
@@ -136,7 +136,7 @@ fn is_subset(a: &Array2<f64>, b: &ArrayView1<f64>, axis: Axis) -> bool
136136
fn sampling_without_replacement_from_a_zero_length_axis_should_panic()
137137
{
138138
let n = 5;
139-
let a = Array::random((0, n), Uniform::new(0., 2.));
139+
let a = Array::random((0, n), Uniform::new(0., 2.).unwrap());
140140
let _samples = a.sample_axis(Axis(0), 1, SamplingStrategy::WithoutReplacement);
141141
}
142142

@@ -145,6 +145,6 @@ fn sampling_without_replacement_from_a_zero_length_axis_should_panic()
145145
fn sampling_with_replacement_from_a_zero_length_axis_should_panic()
146146
{
147147
let n = 5;
148-
let a = Array::random((0, n), Uniform::new(0., 2.));
148+
let a = Array::random((0, n), Uniform::new(0., 2.).unwrap());
149149
let _samples = a.sample_axis(Axis(0), 1, SamplingStrategy::WithReplacement);
150150
}

0 commit comments

Comments
 (0)