Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps): update rand requirement from 0.8 to 0.9 #862

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["Anshul Malik <[email protected]>"]
[dependencies]
num-bigint = { version = "0.4", optional = true }
num-traits = { version = "0.2", optional = true }
rand = "0.8"
rand = "0.9"
nalgebra = "0.33.0"

[dev-dependencies]
Expand Down
4 changes: 4 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
allowed-duplicate-crates = [
"zerocopy",
"zerocopy-derive",
]
6 changes: 3 additions & 3 deletions src/data_structures/veb_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,21 +322,21 @@ mod test {
#[test]
fn test_10_256() {
let mut rng = StdRng::seed_from_u64(0);
let elements: Vec<u32> = (0..10).map(|_| rng.gen_range(0..255)).collect();
let elements: Vec<u32> = (0..10).map(|_| rng.random_range(0..255)).collect();
test_veb_tree(256, elements, Vec::new());
}

#[test]
fn test_100_256() {
let mut rng = StdRng::seed_from_u64(0);
let elements: Vec<u32> = (0..100).map(|_| rng.gen_range(0..255)).collect();
let elements: Vec<u32> = (0..100).map(|_| rng.random_range(0..255)).collect();
test_veb_tree(256, elements, Vec::new());
}

#[test]
fn test_100_300() {
let mut rng = StdRng::seed_from_u64(0);
let elements: Vec<u32> = (0..100).map(|_| rng.gen_range(0..255)).collect();
let elements: Vec<u32> = (0..100).map(|_| rng.random_range(0..255)).collect();
test_veb_tree(300, elements, Vec::new());
}
}
28 changes: 14 additions & 14 deletions src/general/genetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl<Rng: rand::Rng> SelectionStrategy<Rng> for RouletteWheel<Rng> {
return (parents[0], parents[1]);
}
let sum: f64 = fitnesses.iter().sum();
let mut spin = self.rng.gen_range(0.0..=sum);
let mut spin = self.rng.random_range(0.0..=sum);
for individual in population {
let fitness: f64 = individual.fitness().into();
if spin <= fitness {
Expand Down Expand Up @@ -104,7 +104,7 @@ impl<const K: usize, Rng: rand::Rng> SelectionStrategy<Rng> for Tournament<K, Rn
// This means we can draw K random (distinct) numbers between (0..population.len()) and return the chromosomes at the 2 lowest indices
let mut picked_indices = BTreeSet::new(); // will keep indices ordered
while picked_indices.len() < K {
picked_indices.insert(self.rng.gen_range(0..population.len()));
picked_indices.insert(self.rng.random_range(0..population.len()));
}
let mut iter = picked_indices.into_iter();
(
Expand Down Expand Up @@ -185,15 +185,15 @@ impl<

// 3. Apply random mutations to the whole population
for chromosome in self.population.iter_mut() {
if self.rng.gen::<f64>() <= self.mutation_chance {
if self.rng.random::<f64>() <= self.mutation_chance {
chromosome.mutate(&mut self.rng);
}
}
// 4. Select parents that will be mating to create new chromosomes
let mut new_population = Vec::with_capacity(self.population.len() + 1);
while new_population.len() < self.population.len() {
let (p1, p2) = self.selection.select(&self.population);
if self.rng.gen::<f64>() <= self.crossover_chance {
if self.rng.random::<f64>() <= self.crossover_chance {
let child = p1.crossover(p2, &mut self.rng);
new_population.push(child);
} else {
Expand All @@ -220,7 +220,7 @@ mod tests {
Tournament,
};
use rand::rngs::ThreadRng;
use rand::{thread_rng, Rng};
use rand::{rng, Rng};
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::ops::RangeInclusive;
Expand All @@ -240,7 +240,7 @@ mod tests {
impl TestString {
fn new(rng: &mut ThreadRng, secret: String, chars: RangeInclusive<char>) -> Self {
let current = (0..secret.len())
.map(|_| rng.gen_range(chars.clone()))
.map(|_| rng.random_range(chars.clone()))
.collect::<Vec<_>>();

Self {
Expand All @@ -258,16 +258,16 @@ mod tests {
impl Chromosome<ThreadRng, i32> for TestString {
fn mutate(&mut self, rng: &mut ThreadRng) {
// let's assume mutations happen completely randomly, one "gene" at a time (i.e. one char at a time)
let gene_idx = rng.gen_range(0..self.secret.len());
let new_char = rng.gen_range(self.chars.clone());
let gene_idx = rng.random_range(0..self.secret.len());
let new_char = rng.random_range(self.chars.clone());
self.genes[gene_idx] = new_char;
}

fn crossover(&self, other: &Self, rng: &mut ThreadRng) -> Self {
// Let's not assume anything here, simply mixing random genes from both parents
let genes = (0..self.secret.len())
.map(|idx| {
if rng.gen_bool(0.5) {
if rng.random_bool(0.5) {
// pick gene from self
self.genes[idx]
} else {
Expand All @@ -292,7 +292,7 @@ mod tests {
.count() as i32
}
}
let mut rng = thread_rng();
let mut rng = rng();
let pop_count = 1_000;
let mut population = Vec::with_capacity(pop_count);
for _ in 0..pop_count {
Expand Down Expand Up @@ -388,7 +388,7 @@ mod tests {
}
}
fn random_color(rng: &mut ThreadRng) -> ColoredPeg {
match rng.gen_range(0..=5) {
match rng.random_range(0..=5) {
0 => ColoredPeg::Red,
1 => ColoredPeg::Yellow,
2 => ColoredPeg::Green,
Expand All @@ -403,15 +403,15 @@ mod tests {
impl Chromosome<ThreadRng, i32> for CodeBreaker {
fn mutate(&mut self, rng: &mut ThreadRng) {
// change one random color
let idx = rng.gen_range(0..4);
let idx = rng.random_range(0..4);
self.guess[idx] = random_color(rng);
}

fn crossover(&self, other: &Self, rng: &mut ThreadRng) -> Self {
Self {
maker: self.maker.clone(),
guess: std::array::from_fn(|i| {
if rng.gen::<f64>() < 0.5 {
if rng.random::<f64>() < 0.5 {
self.guess[i]
} else {
other.guess[i]
Expand Down Expand Up @@ -443,7 +443,7 @@ mod tests {
mutation_chance: 0.5,
crossover_chance: 0.3,
};
let mut rng = thread_rng();
let mut rng = rng();
let mut initial_pop = Vec::with_capacity(population_count);
for _ in 0..population_count {
initial_pop.push(CodeBreaker {
Expand Down
2 changes: 1 addition & 1 deletion src/graph/depth_first_search_tic_tac_toe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn main() {
Some(x) => {
//Interactive Tic-Tac-Toe play needs the "rand = "0.8.3" crate.
//#[cfg(not(test))]
//let random_selection = rand::thread_rng().gen_range(0..x.positions.len());
//let random_selection = rand::rng().gen_range(0..x.positions.len());
let random_selection = 0;

let response_pos = x.positions[random_selection];
Expand Down
2 changes: 1 addition & 1 deletion src/machine_learning/k_means.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rand::prelude::random;
use rand::random;

fn get_distance(p1: &(f64, f64), p2: &(f64, f64)) -> f64 {
let dx: f64 = p1.0 - p2.0;
Expand Down
4 changes: 2 additions & 2 deletions src/math/quadratic_residue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ pub fn tonelli_shanks(a: i64, odd_prime: u64) -> Option<u64> {
let power_mod_p = |b, e| fast_power(b as usize, e as usize, p as usize) as u128;

// find generator: choose a random non-residue n mod p
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
let n = loop {
let n = rng.gen_range(0..p);
let n = rng.random_range(0..p);
if legendre_symbol(n as u64, p as u64) == -1 {
break n;
}
Expand Down
4 changes: 2 additions & 2 deletions src/sorting/quick_sort_3_ways.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ fn _quick_sort_3_ways<T: Ord>(arr: &mut [T], lo: usize, hi: usize) {
return;
}

let mut rng = rand::thread_rng();
arr.swap(lo, rng.gen_range(lo..hi + 1));
let mut rng = rand::rng();
arr.swap(lo, rng.random_range(lo..hi + 1));

let mut lt = lo; // arr[lo+1, lt] < v
let mut gt = hi + 1; // arr[gt, r] > v
Expand Down
15 changes: 9 additions & 6 deletions src/sorting/sort_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use std::time::Instant;
#[cfg(test)]
pub fn generate_random_vec(n: u32, range_l: i32, range_r: i32) -> Vec<i32> {
let mut arr = Vec::<i32>::with_capacity(n as usize);
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
let mut count = n;

while count > 0 {
arr.push(rng.gen_range(range_l..range_r + 1));
arr.push(rng.random_range(range_l..range_r + 1));
count -= 1;
}

Expand All @@ -18,12 +18,15 @@ pub fn generate_random_vec(n: u32, range_l: i32, range_r: i32) -> Vec<i32> {
#[cfg(test)]
pub fn generate_nearly_ordered_vec(n: u32, swap_times: u32) -> Vec<i32> {
let mut arr: Vec<i32> = (0..n as i32).collect();
let mut rng = rand::thread_rng();
let mut rng = rand::rng();

let mut count = swap_times;

while count > 0 {
arr.swap(rng.gen_range(0..n as usize), rng.gen_range(0..n as usize));
arr.swap(
rng.random_range(0..n as usize),
rng.random_range(0..n as usize),
);
count -= 1;
}

Expand All @@ -44,8 +47,8 @@ pub fn generate_reverse_ordered_vec(n: u32) -> Vec<i32> {

#[cfg(test)]
pub fn generate_repeated_elements_vec(n: u32, unique_elements: u8) -> Vec<i32> {
let mut rng = rand::thread_rng();
let v = rng.gen_range(0..n as i32);
let mut rng = rand::rng();
let v = rng.random_range(0..n as i32);
generate_random_vec(n, v, v + unique_elements as i32)
}

Expand Down