Skip to content

Commit

Permalink
Merge pull request #380 from sparsemat/rand-0.9
Browse files Browse the repository at this point in the history
Upgrade to rand 0.9
  • Loading branch information
mulimoen authored Jan 31, 2025
2 parents e7314f4 + a5ff25b commit 397012c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions sprs-rand/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license.workspace = true
edition.workspace = true

[dependencies]
rand = "0.8.5"
rand_distr = "0.4.3"
rand_pcg = "0.3.1"
rand = "0.9.0"
rand_distr = "0.5.0"
rand_pcg = "0.9.0"
sprs = { version = "0.11.0", path = "../sprs" }
18 changes: 9 additions & 9 deletions sprs-rand/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Random sparse matrix generation
use crate::rand::distributions::Distribution;
use crate::rand::distr::Distribution;
use crate::rand::Rng;
use crate::rand::SeedableRng;
use sprs::indexing::SpIndex;
Expand Down Expand Up @@ -40,7 +40,7 @@ where
let mut data = Vec::with_capacity(exp_nnz);
// sample row indices
for _ in 0..exp_nnz {
indices.push(I::from_usize(rng.gen_range(0..shape.0)));
indices.push(I::from_usize(rng.random_range(0..shape.0)));
// Note: there won't be any correspondence between the data
// sampled here and the row sampled before, but this does not matter
// as we are sampling.
Expand All @@ -65,7 +65,7 @@ where
let end = indptr[row + 1].index();
for _ in start..end {
loop {
let col = I::from_usize(rng.gen_range(0..shape.1));
let col = I::from_usize(rng.random_range(0..shape.1));
let loc = indices[start..].binary_search(&col);
match loc {
Ok(_) => {
Expand All @@ -87,34 +87,34 @@ where
/// Convenient wrapper for the common case of sampling a matrix with standard
/// normal distribution of the nnz values, using a lightweight rng.
pub fn rand_csr_std(shape: (usize, usize), density: f64) -> CsMat<f64> {
let mut rng = rand_pcg::Pcg64Mcg::from_entropy();
let mut rng = rand_pcg::Pcg64Mcg::from_rng(&mut rand::rng());
rand_csr(&mut rng, crate::rand_distr::StandardNormal, shape, density)
}

#[cfg(test)]
mod tests {
use rand::distributions::Standard;
use rand::distr::StandardUniform;
use rand::SeedableRng;
use sprs::CsMat;

#[test]
fn empty_random_mat() {
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
let empty: CsMat<f64> =
super::rand_csr(&mut rng, Standard, (0, 0), 0.3);
super::rand_csr(&mut rng, StandardUniform, (0, 0), 0.3);
assert_eq!(empty.nnz(), 0);
}

#[test]
fn random_csr() {
let mut rng = rand::rngs::StdRng::seed_from_u64(1234);
let mat: CsMat<f64> =
super::rand_csr(&mut rng, Standard, (100, 70), 0.3);
super::rand_csr(&mut rng, StandardUniform, (100, 70), 0.3);
assert!(mat.density() > 0.25);
assert!(mat.density() < 0.35);

let mat: CsMat<f64> =
super::rand_csr(&mut rng, Standard, (1, 10000), 0.3);
super::rand_csr(&mut rng, StandardUniform, (1, 10000), 0.3);
assert!(mat.density() > 0.28);
assert!(mat.density() < 0.32);
}
Expand Down
2 changes: 1 addition & 1 deletion sprs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tempfile = "3.1.0"
bincode = "1.2.0"
tobj = "4.0"
image = { version = "0.25.2", default-features = false, features = ["png"] }
rand = { version = "0.8", default-features = false, features = ["small_rng"] }
rand = { version = "0.9", default-features = false, features = ["small_rng"] }

[[bench]]
name = "suite"
Expand Down

0 comments on commit 397012c

Please sign in to comment.