@@ -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 {
0 commit comments