@@ -92,7 +92,7 @@ func GeneticString(target string, charmap []rune, conf *Conf) (*Result, error) {
92
92
debug := conf .Debug
93
93
94
94
// Just a seed to improve randomness required by the algorithm
95
- rand .Seed ( time .Now ().UnixNano ())
95
+ rnd := rand .New ( rand . NewSource ( time .Now ().UnixNano () ))
96
96
97
97
// Verify that the target contains no genes besides the ones inside genes variable.
98
98
for position , r := range target {
@@ -113,7 +113,7 @@ func GeneticString(target string, charmap []rune, conf *Conf) (*Result, error) {
113
113
for i := 0 ; i < populationNum ; i ++ {
114
114
key := ""
115
115
for x := 0 ; x < utf8 .RuneCountInString (target ); x ++ {
116
- choice := rand .Intn (len (charmap ))
116
+ choice := rnd .Intn (len (charmap ))
117
117
key += string (charmap [choice ])
118
118
}
119
119
pop [i ] = PopulationItem {key , 0 }
@@ -165,18 +165,18 @@ func GeneticString(target string, charmap []rune, conf *Conf) (*Result, error) {
165
165
nChild = 10
166
166
}
167
167
for x := 0.0 ; x < nChild ; x ++ {
168
- parent2 := pop [rand .Intn (selectionNum )]
168
+ parent2 := pop [rnd .Intn (selectionNum )]
169
169
// Crossover
170
- split := rand .Intn (utf8 .RuneCountInString (target ))
170
+ split := rnd .Intn (utf8 .RuneCountInString (target ))
171
171
child1 := append ([]rune (parent1 .Key )[:split ], []rune (parent2 .Key )[split :]... )
172
172
child2 := append ([]rune (parent2 .Key )[:split ], []rune (parent1 .Key )[split :]... )
173
173
// Clean fitness value
174
174
// Mutate
175
- if rand .Float64 () < mutationProb {
176
- child1 [rand .Intn (len (child1 ))] = charmap [rand .Intn (len (charmap ))]
175
+ if rnd .Float64 () < mutationProb {
176
+ child1 [rnd .Intn (len (child1 ))] = charmap [rnd .Intn (len (charmap ))]
177
177
}
178
- if rand .Float64 () < mutationProb {
179
- child2 [rand .Intn (len (child2 ))] = charmap [rand .Intn (len (charmap ))]
178
+ if rnd .Float64 () < mutationProb {
179
+ child2 [rnd .Intn (len (child2 ))] = charmap [rnd .Intn (len (charmap ))]
180
180
}
181
181
// Push into 'popChildren'
182
182
popChildren = append (popChildren , PopulationItem {string (child1 ), 0 })
0 commit comments