Skip to content

Commit

Permalink
Merge pull request #74 from LAPKB/dependabot/cargo/rand-0.9.0
Browse files Browse the repository at this point in the history
Update rand requirement from 0.8.5 to 0.9.0
  • Loading branch information
mhovd authored Feb 5, 2025
2 parents 8aaef4f + 1fe0f8f commit 582d053
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ faer = "0.19.3"
faer-ext = { version = "0.2.0", features = ["nalgebra", "ndarray"] }
pharmsol = "0.7.2"
toml = "0.8.14" #REMOVE
rand = "0.8.5"
rand = "0.9.0"
anyhow = "1.0.86"


Expand Down
35 changes: 20 additions & 15 deletions src/algorithms/routines/initialization/latin.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use anyhow::Result;
use ndarray::prelude::*;
use ndarray::{Array, ArrayBase, OwnedRepr};
use rand::distributions::{Distribution, Uniform};
use rand::prelude::*;
use rand::rngs::StdRng;
use rand::seq::SliceRandom;
use rand::SeedableRng;
use rand::Rng;

/// Generates a 2-dimensional array containing Latin Hypercube Sampling points within the given ranges.
///
Expand Down Expand Up @@ -36,8 +35,7 @@ pub fn generate(
intervals.shuffle(&mut rng);

for i in 0..n_points {
let u = Uniform::new(0.0, 1.0);
let value = u.sample(&mut rng);
let value = rng.random::<f64>();
seq[[i, j]] = min + ((intervals[i] + value) / n_points as f64) * (max - min);
}
}
Expand All @@ -52,15 +50,22 @@ mod tests {
fn test_generate_lhs() {
let result = generate(5, &vec![(0., 1.), (0., 100.), (0., 1000.)], 42).unwrap();
assert_eq!(result.shape(), &[5, 3]);
assert_eq!(
result,
array![
[0.08118035164615534, 97.33000555311399, 729.2901013820507],
[0.8068685635909911, 34.201431099281876, 969.15809885882],
[0.682991369237072, 56.04329822650589, 299.54535567497913],
[0.5474848855448787, 16.367227512053397, 501.1955274259167],
[0.36985032088988323, 72.73656820048906, 11.698952289960252]
]
);
// Check that the values are within the expected range
for i in 0..5 {
assert!(result[[i, 0]] >= 0. && result[[i, 0]] <= 1.);
assert!(result[[i, 1]] >= 0. && result[[i, 1]] <= 100.);
assert!(result[[i, 2]] >= 0. && result[[i, 2]] <= 1000.);
}

// Check that the values are different
for i in 0..5 {
for j in 0..5 {
if i != j {
assert_ne!(result[[i, 0]], result[[j, 0]]);
assert_ne!(result[[i, 1]], result[[j, 1]]);
assert_ne!(result[[i, 2]], result[[j, 2]]);
}
}
}
}
}

0 comments on commit 582d053

Please sign in to comment.