diff --git a/Cargo.toml b/Cargo.toml index 7711f76..8b3efba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,9 +25,9 @@ wasm-bindgen = ["wasm-bindgen_", "chrono/wasmbind"] [dependencies] chrono = "0.4" -rand = { version = "0.8", default-features = false, features = ["getrandom"] } -rand_xoshiro = "0.6" -fixedbitset = { version = "0.4", optional = true } +rand = { version = "0.9", default-features = false, features = ["std", "os_rng"]} +rand_xoshiro = "0.7" +fixedbitset = { version = "0.5", optional = true } smallvec = { version = "1", optional = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] @@ -35,10 +35,10 @@ rayon = "1" [target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies] wasm-bindgen_ = { version = "0.2", package = "wasm-bindgen", optional = true } -getrandom = { version = "0.2", features = ["js", "wasm-bindgen"] } +getrandom = { version = "0.3", features = ["std"] } [dev-dependencies] -criterion = "0.3" +criterion = "0.5" galvanic-assert = "0.8" proptest = "1" version-sync = "0.9" diff --git a/benches/genome_builder.rs b/benches/genome_builder.rs index 8aee4bf..32b5e64 100644 --- a/benches/genome_builder.rs +++ b/benches/genome_builder.rs @@ -4,7 +4,7 @@ extern crate criterion; use criterion::{BenchmarkId, Criterion}; use genevo::random::{get_rng, random_seed}; use rand::{ - distributions::{Bernoulli, Distribution}, + distr::{Bernoulli, Distribution}, Rng, }; @@ -15,7 +15,7 @@ fn generate_vec_of_random_bool_the_functional_way(c: &mut Criterion) { for length in [12, 24, 48, 96] { group.bench_with_input(BenchmarkId::from_parameter(length), &length, |b, length| { b.iter(|| { - let _genome: Vec = (0..*length).map(|_| rng.gen()).collect(); + let _genome: Vec = (0..*length).map(|_| rng.random()).collect(); }) }); } @@ -31,7 +31,7 @@ fn generate_vec_of_random_bool_using_for_loop(c: &mut Criterion) { let length = *length; let mut genome: Vec = Vec::with_capacity(length); for _ in 0..length { - genome.push(rng.gen()); + genome.push(rng.random()); } }) }); diff --git a/examples/queens/main.rs b/examples/queens/main.rs index bcc7b32..9f19b35 100644 --- a/examples/queens/main.rs +++ b/examples/queens/main.rs @@ -101,7 +101,7 @@ impl RandomValueMutation for Pos { { Pos { x: value.x, - y: rng.gen_range(min_value.y..max_value.y), + y: rng.random_range(min_value.y..max_value.y), } } } @@ -117,7 +117,7 @@ impl GenomeBuilder for QueensPositions { (0..NUM_ROWS) .map(|row| Pos { x: row, - y: rng.gen_range(0..NUM_COLS), + y: rng.random_range(0..NUM_COLS), }) .collect() } diff --git a/src/mutation/order.rs b/src/mutation/order.rs index 73a734a..8c4ad2b 100644 --- a/src/mutation/order.rs +++ b/src/mutation/order.rs @@ -43,7 +43,7 @@ where { let genome_length = genome.len(); let num_mutations = - ((genome_length as f64 * self.mutation_rate) + rng.gen::()).floor() as usize; + ((genome_length as f64 * self.mutation_rate) + rng.random::()).floor() as usize; let mut mutated = genome; for _ in 0..num_mutations { let (locus1, locus2) = random_cut_points(rng, genome_length); @@ -90,7 +90,7 @@ where { let genome_length = genome.len(); let num_mutations = - ((genome_length as f64 * self.mutation_rate) + rng.gen::()).floor() as usize; + ((genome_length as f64 * self.mutation_rate) + rng.random::()).floor() as usize; let mut mutated = genome; for _ in 0..num_mutations { let (locus1, locus2) = random_cut_points(rng, genome_length); diff --git a/src/mutation/value.rs b/src/mutation/value.rs index c4c75a4..e3e94b2 100644 --- a/src/mutation/value.rs +++ b/src/mutation/value.rs @@ -3,7 +3,7 @@ use crate::{ operator::{GeneticOperator, MutationOp}, random::{random_index, Rng}, }; -use rand::seq::SliceRandom; +use rand::seq::IndexedRandom; use std::fmt::Debug; #[derive(Clone, Debug, PartialEq)] @@ -92,7 +92,7 @@ where { let genome_length = genome.len(); let num_mutations = - ((genome_length as f64 * mutation_rate) + rng.gen::()).floor() as usize; + ((genome_length as f64 * mutation_rate) + rng.random::()).floor() as usize; let mut mutated = genome; for _ in 0..num_mutations { let index = random_index(rng, genome_length); @@ -129,11 +129,11 @@ mod fixedbitset_random_genome_mutation { { let genome_length = genome.len(); let num_mutations = - ((genome_length as f64 * mutation_rate) + rng.gen::()).floor() as usize; + ((genome_length as f64 * mutation_rate) + rng.random::()).floor() as usize; let mut mutated = genome; for _ in 0..num_mutations { let bit = random_index(rng, genome_length); - let value = rng.gen(); + let value = rng.random(); mutated.set(bit, value); } mutated @@ -167,7 +167,7 @@ mod smallvec_random_genome_mutation { { let genome_length = genome.len(); let num_mutations = - ((genome_length as f64 * mutation_rate) + rng.gen::()).floor() as usize; + ((genome_length as f64 * mutation_rate) + rng.random::()).floor() as usize; let mut mutated = genome; for _ in 0..num_mutations { let index = random_index(rng, genome_length); @@ -197,14 +197,14 @@ macro_rules! impl_random_value_mutation { fn random_mutated(_: $t, min_value: &$t, max_value: &$t, rng: &mut R) -> $t where R: Rng + Sized { - rng.gen_range(*min_value..*max_value) + rng.random_range(*min_value..*max_value) } } )* } } -impl_random_value_mutation!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64); +impl_random_value_mutation!(u8, u16, u32, u64, usize, i8, i16, i32, i64, f32, f64); impl RandomValueMutation for bool { #[inline] @@ -212,7 +212,7 @@ impl RandomValueMutation for bool { where R: Rng + Sized, { - rng.gen_bool(0.5) + rng.random_bool(0.5) } } @@ -321,7 +321,7 @@ where { let genome_length = genome.len(); let num_mutations = - ((genome_length as f64 * mutation_rate) + rng.gen::()).floor() as usize; + ((genome_length as f64 * mutation_rate) + rng.random::()).floor() as usize; let mut mutated = genome; for _ in 0..num_mutations { let index = random_index(rng, genome_length); @@ -368,4 +368,4 @@ macro_rules! impl_breeder_mutation { } } -impl_breeder_mutation!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64); +impl_breeder_mutation!(u8, u16, u32, u64, usize, i8, i16, i32, i64, f32, f64); diff --git a/src/operator/prelude.rs b/src/operator/prelude.rs index 5654f86..8df8a49 100644 --- a/src/operator/prelude.rs +++ b/src/operator/prelude.rs @@ -1,7 +1,7 @@ -pub use crate::selection::{proportionate::*, ranking::*, tournament::*, truncation::*, *}; +pub use crate::selection::{proportionate::*, tournament::*, truncation::*, *}; -pub use crate::recombination::{discrete::*, order::*, *}; +pub use crate::recombination::{discrete::*, *}; -pub use crate::mutation::{order::*, value::*, *}; +pub use crate::mutation::value::*; pub use crate::reinsertion::{elitist::*, random::*, *}; diff --git a/src/population/mod.rs b/src/population/mod.rs index 4a341ab..159076d 100644 --- a/src/population/mod.rs +++ b/src/population/mod.rs @@ -101,7 +101,7 @@ use crate::{ genetic::Genotype, random::{get_rng, random_seed, Prng, Rng, Seed}, }; -use rand::distributions::uniform::SampleUniform; +use rand::distr::uniform::SampleUniform; #[cfg(not(target_arch = "wasm32"))] use rayon; use std::{fmt::Debug, marker::PhantomData}; @@ -323,7 +323,7 @@ impl GenomeBuilder> for BinaryEncodedGenomeBuilder { where R: Rng + Sized, { - (0..self.genome_length).map(|_| rng.gen()).collect() + (0..self.genome_length).map(|_| rng.random()).collect() } } @@ -363,7 +363,7 @@ where R: Rng + Sized, { (0..self.genome_length) - .map(|_| rng.gen_range(self.min_value.clone()..self.max_value.clone())) + .map(|_| rng.random_range(self.min_value.clone()..self.max_value.clone())) .collect() } } diff --git a/src/random/mod.rs b/src/random/mod.rs index 273814f..17b0968 100644 --- a/src/random/mod.rs +++ b/src/random/mod.rs @@ -2,7 +2,7 @@ //! values for specific purposes. pub use rand::{ - distributions::{uniform::SampleUniform, Open01}, + distr::{uniform::SampleUniform, Open01}, seq::SliceRandom, Rng, SeedableRng, }; @@ -19,8 +19,8 @@ pub type Seed = ::Seed; /// Generates a random seed to initialize the `Prng`. pub fn random_seed() -> Seed { - let mut rng = Prng::from_entropy(); - rng.gen() + let mut rng = Prng::from_os_rng(); + rng.random() } /// Returns a new `Prng` initialized with the given seed. @@ -42,7 +42,7 @@ pub fn random_index_from_range(rng: &mut R, min: usize, max: usize) -> usize where R: Rng + Sized, { - rng.gen_range(min..max) + rng.random_range(min..max) } /// Generates two cut points for a slice of given length using the given `Prng`. @@ -64,8 +64,8 @@ where assert!(max >= min + 4); let max_slice = max - min - 2; loop { - let cutpoint1 = rng.gen_range(min..max); - let cutpoint2 = rng.gen_range(min..max); + let cutpoint1 = rng.random_range(min..max); + let cutpoint2 = rng.random_range(min..max); if cutpoint1 < cutpoint2 { if cutpoint2 - cutpoint1 >= max_slice { continue; diff --git a/src/random/tests.rs b/src/random/tests.rs index fc63d8a..b1c1bfc 100644 --- a/src/random/tests.rs +++ b/src/random/tests.rs @@ -144,7 +144,7 @@ mod weighted_distribution { let mut counter = vec![0, 0, 0, 0]; for _ in 0..n_sum { - let random = rng.gen::() * weighted_distribution.sum(); + let random = rng.random::() * weighted_distribution.sum(); let index = weighted_distribution.select(random); counter[index] += 1; } diff --git a/src/recombination/discrete.rs b/src/recombination/discrete.rs index b7858be..4507bab 100644 --- a/src/recombination/discrete.rs +++ b/src/recombination/discrete.rs @@ -58,7 +58,7 @@ where // for each value in the genotype for locus in 0..genome_length { // pick the value of a randomly chosen parent - let random = rng.gen_range(0..num_parents); + let random = rng.random_range(0..num_parents); let value = parents[random][locus].clone(); genome.push(value); } @@ -254,7 +254,7 @@ where let mut p_index = num_parents; loop { loop { - let index = rng.gen_range(0..num_parents); + let index = rng.random_range(0..num_parents); if index != p_index { p_index = index; break; diff --git a/src/types/fmt/tests.rs b/src/types/fmt/tests.rs index 4544193..4f620bf 100644 --- a/src/types/fmt/tests.rs +++ b/src/types/fmt/tests.rs @@ -1,5 +1,5 @@ use super::*; -use chrono::Duration; +use chrono::{Duration, TimeDelta}; use galvanic_assert::matchers::*; #[test] @@ -10,7 +10,7 @@ fn duration_fmt_zero() { #[test] fn duration_fmt_max() { assert_that!( - &Duration::max_value().fmt(), + &TimeDelta::MAX.fmt(), eq("15250284452w 3d 7h 12m 55s".to_string()) ); } @@ -18,7 +18,7 @@ fn duration_fmt_max() { #[test] fn duration_fmt_min() { assert_that!( - &Duration::min_value().fmt(), + &TimeDelta::MIN.fmt(), eq("-15250284452w 3d 7h 12m 55s".to_string()) ); } diff --git a/tests/population_builder.rs b/tests/population_builder.rs index 2db2035..290197a 100644 --- a/tests/population_builder.rs +++ b/tests/population_builder.rs @@ -49,7 +49,7 @@ fn create_population_of_custom_genotype_uniform_at_random() { (0..8) .map(|row| Pos { x: row, - y: rng.gen_range(0..8), + y: rng.random_range(0..8), }) .collect() }