@@ -68,7 +68,7 @@ impl<Rng: rand::Rng> SelectionStrategy<Rng> for RouletteWheel<Rng> {
68
68
return ( parents[ 0 ] , parents[ 1 ] ) ;
69
69
}
70
70
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) ;
72
72
for individual in population {
73
73
let fitness: f64 = individual. fitness ( ) . into ( ) ;
74
74
if spin <= fitness {
@@ -104,7 +104,7 @@ impl<const K: usize, Rng: rand::Rng> SelectionStrategy<Rng> for Tournament<K, Rn
104
104
// This means we can draw K random (distinct) numbers between (0..population.len()) and return the chromosomes at the 2 lowest indices
105
105
let mut picked_indices = BTreeSet :: new ( ) ; // will keep indices ordered
106
106
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 ( ) ) ) ;
108
108
}
109
109
let mut iter = picked_indices. into_iter ( ) ;
110
110
(
@@ -185,15 +185,15 @@ impl<
185
185
186
186
// 3. Apply random mutations to the whole population
187
187
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 {
189
189
chromosome. mutate ( & mut self . rng ) ;
190
190
}
191
191
}
192
192
// 4. Select parents that will be mating to create new chromosomes
193
193
let mut new_population = Vec :: with_capacity ( self . population . len ( ) + 1 ) ;
194
194
while new_population. len ( ) < self . population . len ( ) {
195
195
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 {
197
197
let child = p1. crossover ( p2, & mut self . rng ) ;
198
198
new_population. push ( child) ;
199
199
} else {
@@ -220,7 +220,7 @@ mod tests {
220
220
Tournament ,
221
221
} ;
222
222
use rand:: rngs:: ThreadRng ;
223
- use rand:: { thread_rng , Rng } ;
223
+ use rand:: { rng , Rng } ;
224
224
use std:: collections:: HashMap ;
225
225
use std:: fmt:: { Debug , Formatter } ;
226
226
use std:: ops:: RangeInclusive ;
@@ -240,7 +240,7 @@ mod tests {
240
240
impl TestString {
241
241
fn new ( rng : & mut ThreadRng , secret : String , chars : RangeInclusive < char > ) -> Self {
242
242
let current = ( 0 ..secret. len ( ) )
243
- . map ( |_| rng. gen_range ( chars. clone ( ) ) )
243
+ . map ( |_| rng. random_range ( chars. clone ( ) ) )
244
244
. collect :: < Vec < _ > > ( ) ;
245
245
246
246
Self {
@@ -258,16 +258,16 @@ mod tests {
258
258
impl Chromosome < ThreadRng , i32 > for TestString {
259
259
fn mutate ( & mut self , rng : & mut ThreadRng ) {
260
260
// 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 ( ) ) ;
263
263
self . genes [ gene_idx] = new_char;
264
264
}
265
265
266
266
fn crossover ( & self , other : & Self , rng : & mut ThreadRng ) -> Self {
267
267
// Let's not assume anything here, simply mixing random genes from both parents
268
268
let genes = ( 0 ..self . secret . len ( ) )
269
269
. map ( |idx| {
270
- if rng. gen_bool ( 0.5 ) {
270
+ if rng. random_bool ( 0.5 ) {
271
271
// pick gene from self
272
272
self . genes [ idx]
273
273
} else {
@@ -292,7 +292,7 @@ mod tests {
292
292
. count ( ) as i32
293
293
}
294
294
}
295
- let mut rng = thread_rng ( ) ;
295
+ let mut rng = rng ( ) ;
296
296
let pop_count = 1_000 ;
297
297
let mut population = Vec :: with_capacity ( pop_count) ;
298
298
for _ in 0 ..pop_count {
@@ -388,7 +388,7 @@ mod tests {
388
388
}
389
389
}
390
390
fn random_color ( rng : & mut ThreadRng ) -> ColoredPeg {
391
- match rng. gen_range ( 0 ..=5 ) {
391
+ match rng. random_range ( 0 ..=5 ) {
392
392
0 => ColoredPeg :: Red ,
393
393
1 => ColoredPeg :: Yellow ,
394
394
2 => ColoredPeg :: Green ,
@@ -403,15 +403,15 @@ mod tests {
403
403
impl Chromosome < ThreadRng , i32 > for CodeBreaker {
404
404
fn mutate ( & mut self , rng : & mut ThreadRng ) {
405
405
// change one random color
406
- let idx = rng. gen_range ( 0 ..4 ) ;
406
+ let idx = rng. random_range ( 0 ..4 ) ;
407
407
self . guess [ idx] = random_color ( rng) ;
408
408
}
409
409
410
410
fn crossover ( & self , other : & Self , rng : & mut ThreadRng ) -> Self {
411
411
Self {
412
412
maker : self . maker . clone ( ) ,
413
413
guess : std:: array:: from_fn ( |i| {
414
- if rng. gen :: < f64 > ( ) < 0.5 {
414
+ if rng. random :: < f64 > ( ) < 0.5 {
415
415
self . guess [ i]
416
416
} else {
417
417
other. guess [ i]
@@ -443,7 +443,7 @@ mod tests {
443
443
mutation_chance : 0.5 ,
444
444
crossover_chance : 0.3 ,
445
445
} ;
446
- let mut rng = thread_rng ( ) ;
446
+ let mut rng = rng ( ) ;
447
447
let mut initial_pop = Vec :: with_capacity ( population_count) ;
448
448
for _ in 0 ..population_count {
449
449
initial_pop. push ( CodeBreaker {
0 commit comments