Skip to content

Commit 20b718d

Browse files
committed
Update code for rand 0,9
1 parent 9d59c82 commit 20b718d

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ clap = { version = "4.0", optional = true, features = ["cargo"] }
3535
clap_autocomplete = { version = "0.4", optional = true }
3636
poloto = { version = "19", optional = true, default-features = false }
3737
tagu = "0.1.6"
38-
rand_xorshift = { version = "0.3.0", optional = true }
38+
rand_xorshift = { version = "0.4.0", optional = true }
3939

4040
[features]
4141
default = ["bin", "pretty", "completion", "regression", "ols", "percentile-rand", "generic-impls", "binary_search_rng", "random_subset_regression"]
@@ -87,7 +87,7 @@ pretty = ["bin", "atty", "colored"]
8787
completion = ["clap_autocomplete"]
8888

8989
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
90-
getrandom = { version = "0.2", features = ["js"] }
90+
getrandom = { version = "0.3", features = ["wasm_js"] }
9191

9292
# Build with `--profile production`
9393
[profile.production]

src/percentile.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,9 @@ pub mod pivot_fn {
541541
#[cfg(feature = "percentile-rand")]
542542
#[inline]
543543
pub fn rand<T: Clone, S: SliceSubset<T> + ?Sized>() -> impl FnMut(&mut S) -> Cow<'_, T> {
544-
let mut rng = rand::thread_rng();
544+
let mut rng = rand::rng();
545545
move |slice| {
546-
let idx = rng.sample(rand::distributions::Uniform::new(0_usize, slice.len()));
546+
let idx = rng.random_range(0..slice.len());
547547
// UNWRAP: it's less than `slice.len`.
548548
// We assume `!slice.is_empty()`.
549549
Cow::Borrowed(slice.get(idx).unwrap())
@@ -614,9 +614,9 @@ pub mod cluster {
614614
#[cfg(feature = "percentile-rand")]
615615
#[inline]
616616
pub fn rand() -> impl FnMut(&ClusterList) -> f64 {
617-
let mut rng = rand::thread_rng();
617+
let mut rng = rand::rng();
618618
move |slice| {
619-
let idx = rng.sample(rand::distributions::Uniform::new(0_usize, slice.len()));
619+
let idx = rng.random_range(0..slice.len());
620620
// Panic (index call): it's less than `slice.len`.
621621
// We assume `!slice.is_empty()`.
622622
*slice.index(idx)

src/regression.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,7 +2313,7 @@ pub mod theil_sen {
23132313
let mut s1 = [0.0; 20];
23142314
let mut s2 = [0.0; 20];
23152315

2316-
let mut rng = rand::thread_rng();
2316+
let mut rng = rand::rng();
23172317
rng.fill(&mut s1);
23182318
rng.fill(&mut s2);
23192319

@@ -3495,8 +3495,8 @@ pub mod binary_search {
34953495
// decrease randomness at the end
34963496
let progress = 1.0 - iter as f64 / self.iterations as f64;
34973497
// gen f32 since that takes less bytes
3498-
let rng_factor =
3499-
1. + (2.0 * rng.gen::<f32>() as f64 - 1.) * self.randomness_factor * progress;
3498+
let rng_factor = 1.
3499+
+ (2.0 * rng.random::<f32>() as f64 - 1.) * self.randomness_factor * progress;
35003500

35013501
// for each variable to optimize
35023502
for i in 0..n {
@@ -3573,7 +3573,7 @@ pub mod binary_search {
35733573
impl $name for Options {
35743574
fn $method(&self, predictors: &[f64], outcomes: &[f64]) -> $ret {
35753575
use rand::SeedableRng;
3576-
let mut rng = rand_xorshift::XorShiftRng::from_rng(rand::thread_rng()).unwrap();
3576+
let mut rng = rand_xorshift::XorShiftRng::from_rng(&mut rand::rng());
35773577

35783578
#[cfg(feature = "random_subset_regression")]
35793579
if let Some(random_config) = &self.random_subset_regression {
@@ -3619,7 +3619,7 @@ pub mod binary_search {
36193619
impl $name for Options {
36203620
fn $method(&self, predictors: &[f64], outcomes: &[f64], max_frequency: f64) -> $ret {
36213621
use rand::SeedableRng;
3622-
let mut rng = rand_xorshift::XorShiftRng::from_rng(rand::thread_rng()).unwrap();
3622+
let mut rng = rand_xorshift::XorShiftRng::from_rng(&mut rand::rng());
36233623

36243624
#[cfg(feature = "random_subset_regression")]
36253625
if let Some(random_config) = &self.random_subset_regression {
@@ -3730,7 +3730,7 @@ pub mod binary_search {
37303730
degree: usize,
37313731
) -> PolynomialCoefficients {
37323732
use rand::SeedableRng;
3733-
let mut rng = rand_xorshift::XorShiftRng::from_rng(rand::thread_rng()).unwrap();
3733+
let mut rng = rand_xorshift::XorShiftRng::from_rng(&mut rand::rng());
37343734

37353735
#[cfg(feature = "random_subset_regression")]
37363736
if let Some(random_config) = &self.random_subset_regression {
@@ -3793,7 +3793,7 @@ pub mod binary_search {
37933793
#[test]
37943794
#[cfg(feature = "binary_search_rng")]
37953795
fn two_variable_regression() {
3796-
let mut rng = rand::thread_rng();
3796+
let mut rng = rand::rng();
37973797
let now = std::time::Instant::now();
37983798
let x = [1.3, 4.7, 9.4];
37993799
let y = [4., 5.3, 6.7];
@@ -3822,7 +3822,7 @@ pub mod binary_search {
38223822
#[cfg(feature = "binary_search_rng")]
38233823
fn second_degree_regression() {
38243824
// init thread rng
3825-
let _rng = rand::thread_rng();
3825+
let _rng = rand::rng();
38263826
let now = std::time::Instant::now();
38273827
let x = [1.3, 4.7, 9.4];
38283828
let y = [4., 5.3, 6.7];
@@ -3839,7 +3839,7 @@ pub mod binary_search {
38393839
use rand::SeedableRng;
38403840
// init thread rng
38413841

3842-
let mut rng = rand_xorshift::XorShiftRng::from_rng(rand::thread_rng()).unwrap();
3842+
let mut rng = rand_xorshift::XorShiftRng::from_rng(&mut rand::rng());
38433843
let now = std::time::Instant::now();
38443844
let coeffs = Options::default()
38453845
.max_precision()
@@ -3912,7 +3912,7 @@ pub mod random_subset_regression {
39123912
);
39133913
return None;
39143914
}
3915-
let distribution = rand::distributions::Uniform::new(0, x.len());
3915+
let distribution = rand::distr::Uniform::new(0, x.len()).unwrap();
39163916
let subsets = (0..config.subsets_count)
39173917
.map(|_| {
39183918
let mut new_x = Vec::with_capacity(config.subset_length);

0 commit comments

Comments
 (0)