Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c149ad5

Browse files
dependabot[bot]vil02
andauthored
chore(deps): update rand requirement from 0.8 to 0.9 (TheAlgorithms#862)
* chore(deps): update rand requirement from 0.8 to 0.9 Updates the requirements on [rand](https://github.com/rust-random/rand) to permit the latest version. - [Release notes](https://github.com/rust-random/rand/releases) - [Changelog](https://github.com/rust-random/rand/blob/master/CHANGELOG.md) - [Commits](rust-random/rand@0.8.0...0.9.0) --- updated-dependencies: - dependency-name: rand dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> * fix: update names from `rand` * fix: allow duplicated versions of `zerocopy` and `zerocopy-derive` --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: vil02 <65706193+vil02@users.noreply.github.com>
1 parent d154615 commit c149ad5

File tree

9 files changed

+37
-30
lines changed

9 files changed

+37
-30
lines changed

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ authors = ["Anshul Malik <malikanshul29@gmail.com>"]
77
[dependencies]
88
num-bigint = { version = "0.4", optional = true }
99
num-traits = { version = "0.2", optional = true }
10-
rand = "0.8"
10+
rand = "0.9"
1111
nalgebra = "0.33.0"
1212

1313
[dev-dependencies]

‎clippy.toml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
allowed-duplicate-crates = [
2+
"zerocopy",
3+
"zerocopy-derive",
4+
]

‎src/data_structures/veb_tree.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,21 +322,21 @@ mod test {
322322
#[test]
323323
fn test_10_256() {
324324
let mut rng = StdRng::seed_from_u64(0);
325-
let elements: Vec<u32> = (0..10).map(|_| rng.gen_range(0..255)).collect();
325+
let elements: Vec<u32> = (0..10).map(|_| rng.random_range(0..255)).collect();
326326
test_veb_tree(256, elements, Vec::new());
327327
}
328328

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

336336
#[test]
337337
fn test_100_300() {
338338
let mut rng = StdRng::seed_from_u64(0);
339-
let elements: Vec<u32> = (0..100).map(|_| rng.gen_range(0..255)).collect();
339+
let elements: Vec<u32> = (0..100).map(|_| rng.random_range(0..255)).collect();
340340
test_veb_tree(300, elements, Vec::new());
341341
}
342342
}

‎src/general/genetic.rs‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<Rng: rand::Rng> SelectionStrategy<Rng> for RouletteWheel<Rng> {
6868
return (parents[0], parents[1]);
6969
}
7070
let sum: f64 = fitnesses.iter().sum();
71-
let mut spin = self.rng.gen_range(0.0..=sum);
71+
let mut spin = self.rng.random_range(0.0..=sum);
7272
for individual in population {
7373
let fitness: f64 = individual.fitness().into();
7474
if spin <= fitness {
@@ -104,7 +104,7 @@ impl<const K: usize, Rng: rand::Rng> SelectionStrategy<Rng> for Tournament<K, Rn
104104
// This means we can draw K random (distinct) numbers between (0..population.len()) and return the chromosomes at the 2 lowest indices
105105
let mut picked_indices = BTreeSet::new(); // will keep indices ordered
106106
while picked_indices.len() < K {
107-
picked_indices.insert(self.rng.gen_range(0..population.len()));
107+
picked_indices.insert(self.rng.random_range(0..population.len()));
108108
}
109109
let mut iter = picked_indices.into_iter();
110110
(
@@ -185,15 +185,15 @@ impl<
185185

186186
// 3. Apply random mutations to the whole population
187187
for chromosome in self.population.iter_mut() {
188-
if self.rng.gen::<f64>() <= self.mutation_chance {
188+
if self.rng.random::<f64>() <= self.mutation_chance {
189189
chromosome.mutate(&mut self.rng);
190190
}
191191
}
192192
// 4. Select parents that will be mating to create new chromosomes
193193
let mut new_population = Vec::with_capacity(self.population.len() + 1);
194194
while new_population.len() < self.population.len() {
195195
let (p1, p2) = self.selection.select(&self.population);
196-
if self.rng.gen::<f64>() <= self.crossover_chance {
196+
if self.rng.random::<f64>() <= self.crossover_chance {
197197
let child = p1.crossover(p2, &mut self.rng);
198198
new_population.push(child);
199199
} else {
@@ -220,7 +220,7 @@ mod tests {
220220
Tournament,
221221
};
222222
use rand::rngs::ThreadRng;
223-
use rand::{thread_rng, Rng};
223+
use rand::{rng, Rng};
224224
use std::collections::HashMap;
225225
use std::fmt::{Debug, Formatter};
226226
use std::ops::RangeInclusive;
@@ -240,7 +240,7 @@ mod tests {
240240
impl TestString {
241241
fn new(rng: &mut ThreadRng, secret: String, chars: RangeInclusive<char>) -> Self {
242242
let current = (0..secret.len())
243-
.map(|_| rng.gen_range(chars.clone()))
243+
.map(|_| rng.random_range(chars.clone()))
244244
.collect::<Vec<_>>();
245245

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

266266
fn crossover(&self, other: &Self, rng: &mut ThreadRng) -> Self {
267267
// Let's not assume anything here, simply mixing random genes from both parents
268268
let genes = (0..self.secret.len())
269269
.map(|idx| {
270-
if rng.gen_bool(0.5) {
270+
if rng.random_bool(0.5) {
271271
// pick gene from self
272272
self.genes[idx]
273273
} else {
@@ -292,7 +292,7 @@ mod tests {
292292
.count() as i32
293293
}
294294
}
295-
let mut rng = thread_rng();
295+
let mut rng = rng();
296296
let pop_count = 1_000;
297297
let mut population = Vec::with_capacity(pop_count);
298298
for _ in 0..pop_count {
@@ -388,7 +388,7 @@ mod tests {
388388
}
389389
}
390390
fn random_color(rng: &mut ThreadRng) -> ColoredPeg {
391-
match rng.gen_range(0..=5) {
391+
match rng.random_range(0..=5) {
392392
0 => ColoredPeg::Red,
393393
1 => ColoredPeg::Yellow,
394394
2 => ColoredPeg::Green,
@@ -403,15 +403,15 @@ mod tests {
403403
impl Chromosome<ThreadRng, i32> for CodeBreaker {
404404
fn mutate(&mut self, rng: &mut ThreadRng) {
405405
// change one random color
406-
let idx = rng.gen_range(0..4);
406+
let idx = rng.random_range(0..4);
407407
self.guess[idx] = random_color(rng);
408408
}
409409

410410
fn crossover(&self, other: &Self, rng: &mut ThreadRng) -> Self {
411411
Self {
412412
maker: self.maker.clone(),
413413
guess: std::array::from_fn(|i| {
414-
if rng.gen::<f64>() < 0.5 {
414+
if rng.random::<f64>() < 0.5 {
415415
self.guess[i]
416416
} else {
417417
other.guess[i]
@@ -443,7 +443,7 @@ mod tests {
443443
mutation_chance: 0.5,
444444
crossover_chance: 0.3,
445445
};
446-
let mut rng = thread_rng();
446+
let mut rng = rng();
447447
let mut initial_pop = Vec::with_capacity(population_count);
448448
for _ in 0..population_count {
449449
initial_pop.push(CodeBreaker {

‎src/graph/depth_first_search_tic_tac_toe.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn main() {
110110
Some(x) => {
111111
//Interactive Tic-Tac-Toe play needs the "rand = "0.8.3" crate.
112112
//#[cfg(not(test))]
113-
//let random_selection = rand::thread_rng().gen_range(0..x.positions.len());
113+
//let random_selection = rand::rng().gen_range(0..x.positions.len());
114114
let random_selection = 0;
115115

116116
let response_pos = x.positions[random_selection];

‎src/machine_learning/k_means.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rand::prelude::random;
1+
use rand::random;
22

33
fn get_distance(p1: &(f64, f64), p2: &(f64, f64)) -> f64 {
44
let dx: f64 = p1.0 - p2.0;

‎src/math/quadratic_residue.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ pub fn tonelli_shanks(a: i64, odd_prime: u64) -> Option<u64> {
152152
let power_mod_p = |b, e| fast_power(b as usize, e as usize, p as usize) as u128;
153153

154154
// find generator: choose a random non-residue n mod p
155-
let mut rng = rand::thread_rng();
155+
let mut rng = rand::rng();
156156
let n = loop {
157-
let n = rng.gen_range(0..p);
157+
let n = rng.random_range(0..p);
158158
if legendre_symbol(n as u64, p as u64) == -1 {
159159
break n;
160160
}

‎src/sorting/quick_sort_3_ways.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ fn _quick_sort_3_ways<T: Ord>(arr: &mut [T], lo: usize, hi: usize) {
77
return;
88
}
99

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

1313
let mut lt = lo; // arr[lo+1, lt] < v
1414
let mut gt = hi + 1; // arr[gt, r] > v

‎src/sorting/sort_utils.rs‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use std::time::Instant;
44
#[cfg(test)]
55
pub fn generate_random_vec(n: u32, range_l: i32, range_r: i32) -> Vec<i32> {
66
let mut arr = Vec::<i32>::with_capacity(n as usize);
7-
let mut rng = rand::thread_rng();
7+
let mut rng = rand::rng();
88
let mut count = n;
99

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

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

2323
let mut count = swap_times;
2424

2525
while count > 0 {
26-
arr.swap(rng.gen_range(0..n as usize), rng.gen_range(0..n as usize));
26+
arr.swap(
27+
rng.random_range(0..n as usize),
28+
rng.random_range(0..n as usize),
29+
);
2730
count -= 1;
2831
}
2932

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

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /