-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutator_random.go
51 lines (43 loc) · 1.18 KB
/
mutator_random.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package mutator
import (
"math/rand"
)
// randomChar devuelve un carácter aleatorio basado en la configuración de probabilidades.
func (m *Mutator) randomChar(mutationType MutationType) rune {
var charSet string
// Construir charSet basado en las configuraciones de MutationType
if mutationType.Any {
charSet = m.GlobalCharSet
} else {
if mutationType.Letters {
charSet += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
}
if mutationType.Numbers {
charSet += "0123456789"
}
if mutationType.Special {
charSet += "!@#$%^&*()_+-=[]{}|;:',.<>/?~ "
}
if mutationType.CharSet != "" {
charSet = mutationType.CharSet
}
}
charProbabilities := mutationType.CharProbabilities
if len(charProbabilities) == 0 {
charProbabilities = m.GlobalCharProb
}
var chars []rune
for char, prob := range charProbabilities {
if rand.Float64() < prob && containsRune(charSet, char) {
chars = append(chars, char)
}
}
if len(chars) > 0 {
return chars[rand.Intn(len(chars))]
}
if len(charSet) > 0 {
return rune(charSet[rand.Intn(len(charSet))])
}
// Default fallback: use the global character set
return rune(m.GlobalCharSet[rand.Intn(len(m.GlobalCharSet))])
}